【发布时间】:2023-03-24 19:49:02
【问题描述】:
我想在我的实体保存中执行不带引号的 htmlspecialchars。
代码:
$post['body'] = htmlentities($this->request->getData('body'), ENT_NOQUOTES);
验证器在保存时不起作用。
如何让它发挥作用?
谢谢
【问题讨论】:
标签: cakephp quotes htmlspecialchars
我想在我的实体保存中执行不带引号的 htmlspecialchars。
代码:
$post['body'] = htmlentities($this->request->getData('body'), ENT_NOQUOTES);
验证器在保存时不起作用。
如何让它发挥作用?
谢谢
【问题讨论】:
标签: cakephp quotes htmlspecialchars
h() 方法只是 htmlspecialchars() 的包装器,更多信息可以在 Cake API https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#h 中找到
h(string $text, boolean $double = true, string $charset = null)
总而言之,这些函数的第一个参数是一个字符串,而您正在传递一个数据数组。如果你想去掉那些你可以用你自己或个人覆盖patchEntity() 在每个元素上运行特殊的字符函数
编辑:添加更清晰的示例以保持验证
您可以使用 withData 设置响应数据并仍然使用补丁实体。例如。
$data = $this->request->withData('body', htmlentities($this->request->getData('body'), ENT_NOQUOTES))
然后使用新的请求对象修补/验证实体。
patchEntity($post, $data)
【讨论】:
$data = $this->request->withData('body', htmlentities($this->request->getData('body'), ENT_NOQUOTES)) 然后使用带有 $data 的补丁实体作为请求。我会用一个更清晰的例子来编辑我的帖子