Zend 表单 setRequired(true) 或 addValidator(NotEmpty) [英] Zend form setRequired(true) or addValidator(NotEmpty)

查看:51
本文介绍了Zend 表单 setRequired(true) 或 addValidator(NotEmpty)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个的行为或输出之间有什么真正的区别吗?在我看来,他们做同样的事情.

Is there any real difference between the behavior or output of these 2. They look to me like they do the same thing.

->addValidator('NotEmpty')   

->setRequired(true)

推荐答案

是的,有区别.如果不需要元素,即使您验证的数据中缺少整个值,它也会验证.该值仅在确定它存在后才根据已注册的验证器进行验证.NotEmpty 仅当该字段存在但为空时验证器才会失败.

Yes, there's a difference. If an element is not required, it'll validate even if the whole value is missing from the data you validate against. The value is only validated against registered validators after it's been determined that it exists. NotEmpty validator will only fail if the field is present, but is empty.

另外,没有必要自己添加 NotEmpty 验证器,默认情况下 Zend 会自动为元素插入 NotEmpty 验证器,如果元素是必需的.因此,有效地执行 ->setRequired(true) 与执行 ->setRequired(true)->addValidator('NotEmpty') 相同.您可以使用 ->setAutoInsertNotEmptyValidator(false) 关闭此行为.

Also, it's not necessary to add NotEmpty validator yourself, by default Zend auto inserts NotEmpty validator for elements, if the element is required. So effectively doing ->setRequired(true) is same as doing ->setRequired(true)->addValidator('NotEmpty'). You can turn off this behavior with ->setAutoInsertNotEmptyValidator(false).

这篇关于Zend 表单 setRequired(true) 或 addValidator(NotEmpty)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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