Zend Framework notEmpty 验证器 setRequired [英] Zend Framework notEmpty validator setRequired
问题描述
我查看了其他问题/谷歌搜索了这个.我的问题是,当我提交带有带有 notEmpty
验证器的空文本框的表单时,它不会触发任何错误.
I have looked at other questions/Googled this. My problem is that when I submit my form with empty textboxes that have a notEmpty
validator, it triggers no error.
首先,我想看看我是否理解notEmpty
和setRequired
之间的区别.据我了解,如果提交元素并且值为空,则 notEmpty
验证器会给出错误.也就是说,如果 POST 数据中不存在条目(对于表单),那么如果该元素不是 required,则不会生成错误.就像它只在元素是 set 时运行.setRequired
方法将在幕后自动添加一个 notEmpty
验证器,除非另有说明.这确保元素的条目必须存在并且不能为空.正确吗?
First, I would like to see if I understand the difference between notEmpty
and setRequired
. As I understand, the notEmpty
validator gives an error if an element is submitted and the value is empty. That is, if an entry does not exist in the POST data (for forms), then it does not generate an error if the element is not required. Like it only runs if the element is set. The setRequired
method will automatically add a notEmpty
validator behind the scenes unless told otherwise. This ensures that an entry for an element must exist and it must not be empty. Correct?
现在,我尝试在我添加了 notEmpty
验证器的表单中使用此逻辑,如下所示:
Now, I tried to use this logic in my form where I added notEmpty
validators, like so:
$username = new Zend_Form_Element_Text('txtUsername');
$username->addValidator(new Zend_Validate_NotEmpty(), true);
其他文本字段也是如此.如果我提交表单而不输入值,我的 POST 数据如下所示:
And the same for other text fields. If I submit my form without entering a value, my POST data looks like this:
(
[txtUsername] =>
[txtPassword] =>
[txtRepeatPassword] =>
)
但是,isValid
仍然计算为 true
并且不会生成错误消息.为什么?notEmpty
验证器是否应该确定这些值是空的,因为该元素在 POST 数据中有一个条目?如果我使用 setRequired(true)
,它会按照我想要的方式工作.当值为空字符串时,它确定用户名不为空,这让我感到困惑.:-)
However, isValid
still evaluates to true
and no error messages are generated. Why? Shouldn't the notEmpty
validator determine that the values are empty since the element has an entry in the POST data? If I use setRequired(true)
, it works how I want. It just messes with my mind that it determines that the username is not empty when the value is an empty string. :-)
提前致谢.
推荐答案
我同意这有点令人困惑.原因是所有表单元素都有一个 allowEmpty
标志(默认为 true),如果值为空,则跳过验证.它以这种方式工作,因为否则,将验证器添加到可选元素将是一个真正的痛苦,例如:
I agree that this is a little confusing. The reason is that all form elements have an allowEmpty
flag (true by default), that skips validation if the value is blank. It works this way because otherwise, adding validators to optional elements would be a real pain, e.g.:
$dateOfBirth = new Zend_Form_Element_Text('dob');
$dateOfBirth->setLabel('Date of birth (optional)');
$dateOfBirth->addValidator(new Zend_Validate_Date());
$form->addElement($dateOfBirth);
如果用户选择不输入他们的出生日期,这将始终无法通过验证,因为 '' 不是有效日期.使用 allowEmpty
标志默认情况下,它会按您的预期工作(它仅在输入日期时验证日期).
this would always fail validation if the user chose not to enter their DOB, as '' is not a valid date. With the allowEmpty
flag on by default it works as you would expect (it only validates the date when one has been entered).
调用 setRequired(true)
添加一个 NotEmpty
验证器,但它也将 allowEmpty
标志设置为 false.我建议你坚持这种方法.或者,您也可以通过设置标志来开始工作:
Calling setRequired(true)
adds a NotEmpty
validator but it also sets the allowEmpty
flag to false. I would recommend you stick with this approach. Alternatively you can get your way to work by setting the flag as well:
$username = new Zend_Form_Element_Text('txtUsername');
$username->addValidator(new Zend_Validate_NotEmpty());
$username->setAllowEmpty(false);
这篇关于Zend Framework notEmpty 验证器 setRequired的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!