当出现错误时,如何将类应用到 Zend Framework 装饰器?
现在,我将 Zend 表单元素包裹在列表项中。形式正在按其应有的方式验证或纠正错误。但我想在验证失败时将一个类应用于违规元素的列表项(但仅应用于违规元素的列表项)。示例:
这个字段有有效的输入
<li>
<input type="text" name="address2" id="address2" value="" size="25">
</li>
,但这个没有
<li class="error">
<input type="text" name="address2" id="address2" value="" size="25">
</li>
我能看到的唯一方法是扩展 Zend_Form_Element,并重写 isValid(),然后扩展我的表单所需的从 Zend_Form_Element 派生的所有类。
必须有更好的方法。
正确的?
我缺少什么?
谢谢
Right now, I have Zend form elements wrapped in list-items. Form is validating or coughing up errors as it should. But I'd like to apply a class to the list-items of offending elements (but only to the list-items offending elements) on failed validation. Example:
This field has valid input
<li>
<input type="text" name="address2" id="address2" value="" size="25">
</li>
but this one does not
<li class="error">
<input type="text" name="address2" id="address2" value="" size="25">
</li>
The only way I can see to do this is to extend Zend_Form_Element, and override isValid() and then extend all classes derived from Zend_Form_Element I need for my form(s).
There has got to be a better way.
Right?
What am I missing?
THANKS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您最好根据表单元素的状态修改 HtmlTag 装饰器。
验证时可能有很多不同的地方可以调用它。您可以在您的视图中执行此操作,或者在构建表单时检测每个字段中是否存在错误。
您可能还想考虑创建自己的装饰器。
这些帖子此处和这里很好地涵盖了 Zend_Form 装饰器过于复杂的世界。
I think you're better off modifying the HtmlTag decorator based on the state of the form element.
There are probably a number of different places you can invoke this when validation. You could do it in your view, or detect the presence of errors on each field when you construct the form.
You may also want to look into creating your own decorators as well.
These posts here and here do a good job of covering the overly-complex world of Zend_Form decorators.