简体   繁体   English

Zend Framework notEmpty验证程序setRequired

[英]Zend Framework notEmpty validator setRequired

I have looked at other questions /Googled this. 我看了其他问题 / Googled这个。 My problem is that when I submit my form with empty textboxes that have a notEmpty validator, it triggers no error. 我的问题是,当我使用具有notEmpty验证器的空文本框提交表单时,它不会触发任何错误。

First, I would like to see if I understand the difference between notEmpty and setRequired . 首先,我想看看我是否了解notEmptysetRequired之间的区别。 As I understand, the notEmpty validator gives an error if an element is submitted and the value is empty. 据我了解,如果提交了一个元素且该值为空,则notEmpty验证器将给出错误。 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 . 也就是说,如果一个条目不存在于POST数据存在(形式),那么它不如果不需要该元件产生一个错误。 Like it only runs if the element is set . 就像它只在设置了元素时运行。 The setRequired method will automatically add a notEmpty validator behind the scenes unless told otherwise. 除非另有说明,否则setRequired方法将在幕后自动添加notEmpty验证器。 This ensures that an entry for an element must exist and it must not be empty. 这样可以确保元素的条目必须存在,并且不能为空。 Correct? 正确?

Now, I tried to use this logic in my form where I added notEmpty validators, like so: 现在,我尝试在表单中使用此逻辑,在其中添加notEmpty验证器,如下所示:

$username = new Zend_Form_Element_Text('txtUsername');
$username->addValidator(new Zend_Validate_NotEmpty(), true);

And the same for other text fields. 与其他文本字段相同。 If I submit my form without entering a value, my POST data looks like this: 如果我提交表单时未输入值,则我的POST数据如下所示:

(
    [txtUsername] => 
    [txtPassword] => 
    [txtRepeatPassword] => 
)

However, isValid still evaluates to true and no error messages are generated. 但是, isValid仍评估为true并且不会生成任何错误消息。 Why? 为什么? Shouldn't the notEmpty validator determine that the values are empty since the element has an entry in the POST data? 由于元素在POST数据中有一个条目,因此notEmpty验证程序是否不应该确定值是否为空? If I use setRequired(true) , it works how I want. 如果我使用setRequired(true) ,它可以按我的方式工作。 It just messes with my mind that it determines that the username is not empty when the value is an empty string. 我的想法是,当值是一个空字符串时,它确定用户名不为空,这使我很困惑。 :-) :-)

Thanks in advance. 提前致谢。

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. 原因是所有表单元素都有一个allowEmpty标志(默认情况下为true),如果该值为空,则跳过验证。 It works this way because otherwise, adding validators to optional elements would be a real pain, eg: 之所以这样工作是因为,否则,将验证器添加到可选元素将是一个真正的难题,例如:

$dateOfBirth = new Zend_Form_Element_Text('dob');
$dateOfBirth->setLabel('Date of birth (optional)');
$dateOfBirth->addValidator(new Zend_Validate_Date());
$form->addElement($dateOfBirth);

this would always fail validation if the user chose not to enter their DOB, as '' is not a valid date. 如果用户选择不输入其DOB,这将始终无法通过验证,因为“”不是有效日期。 With the allowEmpty flag on by default it works as you would expect (it only validates the date when one has been entered). 默认情况下, allowEmpty标志处于启用状态,它可以按您预期的方式工作(它仅在输入日期时验证日期)。

Calling setRequired(true) adds a NotEmpty validator but it also sets the allowEmpty flag to false. 调用setRequired(true)会添加一个NotEmpty验证器,但也会将allowEmpty标志设置为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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM