Zend 表单 - 为什么验证失败?

发布于 2024-12-20 16:14:17 字数 779 浏览 2 评论 0原文

我有一个表单中必需的密码字段:

$pwd = $this->CreateElement('password', 'pwd')
        ->setLabel('Change Password')
        ->setRequired(true)
        ->addValidator('StringLength', false, array(6, 16))
        ->addValidator('regex', false, array('pattern' => $allowed_pattern))
        ->addErrorMessages(array(
            'regexNotMatch' => "Only alpha-numeric characters and some punctuation are allowed.",
        ))
        ->addFilter('StringTrim');

但在编辑表单的情况下,我不想需要此字段。 我在控制器中使用:

    $form->getElement('pwd')->setRequired(false);

在将表单传递给视图之前, 。当我检查此表单元素的属性时,我将 _allowEmpty 设置为 1,并将 _required 设置为 0。但是,当提交表单(带有空字段)时,它无法验证此字段,并返回上面的 RexEx 验证器的错误消息。

任何想法为什么字段在这种情况下验证失败?

I have a password field that is required in the form:

$pwd = $this->CreateElement('password', 'pwd')
        ->setLabel('Change Password')
        ->setRequired(true)
        ->addValidator('StringLength', false, array(6, 16))
        ->addValidator('regex', false, array('pattern' => $allowed_pattern))
        ->addErrorMessages(array(
            'regexNotMatch' => "Only alpha-numeric characters and some punctuation are allowed.",
        ))
        ->addFilter('StringTrim');

But in the case of an EDIT to the form, I don't want to require this field. There I use:

    $form->getElement('pwd')->setRequired(false);

in my controller before passing the form to the view. When I check the properties of this form element, I have _allowEmpty set to 1 and _required set to 0. However when the form is submitted (with empty field) it fails validation on this field and returns the error message for the RexEx validator above.

Any ideas why field fails validation under these circumstances?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2024-12-27 16:14:17

您仍然可以改进创建表单元素的方法,

  1. 删除此处的setRequired(true),并在您想要使用此表单元素的地方提及 true 或 false(我认为您指定为 true 和 false

  2. 当您提交空表单时,它会返回 addValidator('StringLength', false, array(6, 16)) 错误,因为,

  3. 如果您指定 addErrorMessages() 函数,它将禁用所有其他验证器消息并返回其数组消息,因为你忘记提及为字符串长度添加错误消息元素。

查看以下示例并尝试将其应用于您的案例。

$samplepassword=$this->createElement('samplepassword','samplepassword');
    $samplepassword->setLabel('Password')
            ->addValidator('NotEmpty')
            ->addValidator('StringLength',false,array('min'=>6,'max'=>16,'encoding' => 'UTF-8','messages'=>array(
                                            'stringLengthTooShort' => 'Passwords must be 6 char mi',
                                            'stringLengthTooLong' => 'Passwords must be no longer 16 something like that'
                                        )))
            ->addValidator('regex', false, array('pattern' => $allowedstring,  messages'=>array('regexNotMatch'=>'Passwords should alphabet or something')));

Still you can improve the method of creating form element,

  1. Remove the setRequired(true) here and mention true or false where you want use this form element(I think you are specify to true and false for the same element).

  2. When you submit empty form it is returning addValidator('StringLength', false, array(6, 16)) error because,

  3. if you specify addErrorMessages() function it disables all other validator messages and retures its array messages and because you forgot to mention add error message element for string lentght.

Look at the follow example to and try to apply it for you case.

$samplepassword=$this->createElement('samplepassword','samplepassword');
    $samplepassword->setLabel('Password')
            ->addValidator('NotEmpty')
            ->addValidator('StringLength',false,array('min'=>6,'max'=>16,'encoding' => 'UTF-8','messages'=>array(
                                            'stringLengthTooShort' => 'Passwords must be 6 char mi',
                                            'stringLengthTooLong' => 'Passwords must be no longer 16 something like that'
                                        )))
            ->addValidator('regex', false, array('pattern' => $allowedstring,  messages'=>array('regexNotMatch'=>'Passwords should alphabet or something')));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文