Zend 表单和 Zend 过滤器 HtmlEntities [英] Zend form and Zend filter HtmlEntities

查看:21
本文介绍了Zend 表单和 Zend 过滤器 HtmlEntities的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个字段的注册表单.其中之一看起来像这样:

I have a registration form with a few fields. One of them looks like this:

        $first_name = new Zend_Form_Element_Text('first_name');
        $first_name ->setLabel("First name")
                    ->setRequired(true)
                    ->addFilter(new Zend_Filter_HtmlEntities());

我使用相同的表单来编辑用户的详细信息.问题在于 Zend_filter_HtmlEntities.当我将表单的数据发送到数据库时,它会完成这项工作,它将 html 特殊字符替换为它们的替代字符.然而,当我初始化这个表单并从数据库记录中给它默认值时,Zend_filter_HtmlEntities 再次过滤这些值,我的输入字段中出现了一些垃圾.

I use the same form for editing user's details. The problem is with the Zend_filter_HtmlEntities. It does the job when I send the form's data to the database, it replaces html special chars with their alternatives. However, when I initialise this form and give it default values from a database record, Zend_filter_HtmlEntities filters those values again and I get some garbage in my input fields.

例如,在名字输入字段中,我得到的不是 Name,而是 <b>姓名

For example, in first name input field, instead of <b>Name, I get &amp;lt;b&amp;gt; Name

这意味着当使用默认值呈现表单时,将再次应用元素过滤器并且

因为 &lt;:(

Which means that when the form is rendered with default values, element filters are applied again and < because &lt; :(

除了在数据传递到表单之前重新格式化数据之外,是否有优雅的解决方案?

Is there an elegant solution to this problem, apart from reformatting the data before it gets passed to the form?

推荐答案

添加 Zend_Filter_HtmlEntities 过滤器,我会完全避免.相反,我会担心只有在向用户显示数据时才转义 html 实体.

Adding the Zend_Filter_HtmlEntities filter, I would avoid entirely. Instead, I'd worry about escaping html entities only when displaying the data back to the user.

这篇关于Zend 表单和 Zend 过滤器 HtmlEntities的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆