【问题标题】:CakePHP Validator not working form entityCakePHP Validator 不工作表单实体
【发布时间】:2023-03-24 19:49:02
【问题描述】:

我想在我的实体保存中执行不带引号的 htmlspecialchars。

代码:

$post['body'] = htmlentities($this->request->getData('body'), ENT_NOQUOTES);

验证器在保存时不起作用。

如何让它发挥作用?

谢谢

【问题讨论】:

    标签: cakephp quotes htmlspecialchars


    【解决方案1】:

    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)
    

    【讨论】:

    • 如果我尝试 $post['body'] = htmlentities($this->request->getData('body'), ENT_NOQUOTES);验证器不起作用。有什么方法可以让它工作 - 即使 body 与 getData 相同?
    • 可以使用 withData 设置响应数据,仍然使用补丁实体。例如$data = $this->request->withData('body', htmlentities($this->request->getData('body'), ENT_NOQUOTES)) 然后使用带有 $data 的补丁实体作为请求。我会用一个更清晰的例子来编辑我的帖子
    • 感谢您的回复。使用 $reply['body'] = $this->request->withData('body', htmlentities($this->request->getData('body'), ENT_NOQUOTES)) 时出现无法将值转换为字符串;
    • @KlaasWaas withData() 不会返回一个字符串,如果我没记错的话它会返回一个响应对象。看看我更新的例子,它显示了正确的用法。
    • 抱歉浪费了您的时间。我现在在视图上使用 htmlentities。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多