Zend form setRequired(true) or addValidator(NotEmpty)

£可爱£侵袭症+ 提交于 2020-01-13 08:01:07

问题


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)

回答1:


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.

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).



来源:https://stackoverflow.com/questions/3871460/zend-form-setrequiredtrue-or-addvalidatornotempty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!