【问题标题】:Strange form validation behavior in Zend Framework 2Zend Framework 2 中奇怪的表单验证行为
【发布时间】:2012-11-28 21:53:29
【问题描述】:

在提交和验证 Zend Framework 2 表单时,我遇到了奇怪的行为。我已将表单缩减为三个元素来说明我的意思,但其他元素也存在问题。

我有两个元素;电子邮件和性别。我没有为我的表单指定了InputFilter,所以我的逻辑告诉我我的表单是有效的。但是,当我提交表单并进行验证检查时,电子邮件和性别元素会给出Value is required and can't be empty 错误。但是,我没有明确表示它们是必需的,不能为空。

我的电子邮件元素是Zend\Form\Element\Email 类型,我怀疑这可能与它有关,因为如果我将其更改为Zend\Form\Element\Text,一切都很好。但是,如果我想要一个不需要的电子邮件字段怎么办?

接下来,我的性别选择元素给了我同样的错误。我设置了空选项,但我认为这只是为了使用默认值,例如“选择您的性别”,但它没有添加必需的约束。看来,如果我删除此空选项,则不会收到此错误。这个错误也很奇怪的是,如果我没有选择性别并提交我的表单,那么在回发的下拉列表中选择了一个不同的选项。在这种情况下,我提交的表单带有空选项(“选择您的性别”),但是当刷新表单时,选定的选项会更改为“女性”。 “女性”选项的值为 0,但如果我将值更改为 3,例如,它可以正常工作。但是,如果我需要在我的选项之一中设置值为 0 怎么办?也许它是由类型转换或其他原因引起的。我真的不知道。

下面是我的代码。我写了一些cmets,所以可能更容易理解。

表单类摘录:

// E-mail (generates "Value is required and can't be empty" - but what if the field should be optional? Where is this validator added?
        $email = new Element\Email('email'); // If I change to "Element\Text", there are no problems
        $email->setLabel('E-mail address');

        $email->setLabelAttributes(array(
                                        'for' => $email->getName(),
                                        'class' => 'some class'
                                   ));

        $email->setAttributes(array(
                                   'id' => $email->getName(),
                                   'placeholder' => 'E.g. andy@gmail.com',
                                   'class' => 'some class'
                              ));

// Gender dropdown (generates "Value is required and can't be empty error")
        $gender = new Element\Select('gender');
        $gender->setLabel('Gender');
        $gender->setEmptyOption('Choose your gender'); // Remove this and the error disappears

        $gender->setLabelAttributes(array(
                                    'for' => $gender->getName(),
                                    'class' => 'some class'
                               ));

        $gender->setAttributes(array(
                                    'id' => $gender->getName(),
                                    'class' => 'some class'
                               ));

        $gender->setValueOptions(array(
                                      '1' => 'Male',
                                      '0' => 'Female' // This is selected on postback when the empty option is selected. If I change the value to 3, for instance, it works fine
                                 ));

$this->add($email);
        $this->add($gender);

控制器:

public function registerAction() {
        $registerForm = new RegisterForm();
        $request = $this->getRequest();

        if ($request->isPost()) {
            $registerForm->setData($request->getPost());

            if ($registerForm->isValid()) {
                // Nothing here yet
            }

        }

        return new ViewModel(array('form' => $registerForm));
    }

查看脚本摘录:

$form = $this->form;
$email = $form->get('email');
$gender = $form->get('gender');

// E-mail
echo $this->formLabel()->openTag($email) . $email->getLabel() . $this->formLabel()->closeTag();
echo $this->formInput($email);
echo $this->formElementErrors($email);

// Gender
echo $this->formLabel()->openTag($gender) . $gender->getLabel() . $this->formLabel()->closeTag();
echo $this->formSelect($gender);
echo $this->formElementErrors($gender);

如果我没有很好地解释这些问题,我深表歉意,但这真的很难解释,因为这对我来说似乎很奇怪。我查看了文档,但找不到任何有用的东西(我认为 ZF2 文档仍然很差)。

【问题讨论】:

    标签: php validation zend-form zend-framework2


    【解决方案1】:

    许多人讨厌 ZF1.x 中用于装饰器的表单,但我仍然喜欢它。 ZF2 对我来说最糟糕的部分是你基本上可以扔掉你所有的知识,因为就像 ZF2 中的很多东西一样,它已经完全改变了。

    有新的 (HTML5) 电子邮件元素,但它带有硬编码的必需约束。选择元素也会发生类似的事情。在 ZF1 中,您在元素上有 setRequired() 方法,但现在它被移回表单中的“输入”。换句话说,您必须先获取输入。

    $form->prepare();  // this is another new thing and necessary
    
    $input = $form->getInputFilter();
    $e_filter = $input->get('email');
    $e_filter->setRequired(false);
    
    $g_filter = $input->get('gender');
    $g_filter->setRequired(false);
    

    现在您的表单应该接受空提交。

    更新:关于回发问题。 这完全取决于您使用setData() 设置的数据。这可以是 POST 数据,也可以是来自会话的重定向数据。如果您在没有选择的情况下提交,则您获得的 POST 数据不应包含任何“性别”数据,您在表单中设置的数据也应如此。

    更新 2 看起来根本问题是提交了一个空的性别值并且 POST 值始终是字符串,因此一个空值是长度为 0 的字符串类型。查看Form\View\Helper\FormSelect 和选项的呈现你会发现@987654327 @ 处理选定属性的函数。不幸的是,他们没有设置严格标志,没有0(零)值匹配一个空字符串。

    $post = array('gender' => '');
    $bool = in_array(0, $post);        // this is true
    $bool = in_array(0, $post, true);  // but this is false
    

    因此,在将其添加到 setData() 之前,从 POST 中过滤/删除此空值或将空字符串更改为 NULL,然后它将作为选定选项删除。此外,如果您当然可以在 Zend 类中添加/更改严格选项(我不会这样做)。

    【讨论】:

    • 非常感谢。这有点像我怀疑的,但我不敢相信他们这样做了。对我来说完全是无稽之谈。现在我必须明确声明某些字段不是必需的,但对其他人来说这是正常行为。为每种类型的元素设置不同的默认行为似乎很荒谬。在我的输入上使用 setRequired 就可以了。如果在提交时选择了空选项,您是否知道为什么我的性别选择元素在回发时选择值为 0 的选项? PHP 代码在问题中,here is the markup.
    • @andy124 不要让我开始做所有的决定。我认为他们只是很生气,因为许多人将它与 Ruby on Rails 进行比较,而 ZF1 并没有那么复杂。无论如何,关于您的回发,请在一两分钟内查看我在答案中的更新。
    • 是的,这是有道理的。我仔细检查过,我的代码中没有任何内容可以更改甚至读取该值。我的一个实验对我来说表明问题不在我的尽头。 Again, I have to refer you to Pastebin for the code。 :-) 我已接受您的回答,但如果您能查看此评论中的链接,我将不胜感激。非常感谢您迄今为止的帮助。
    • 是的,我怀疑这是问题所在。动态类型就这么多。谢谢 - 先生,您太棒了!
    【解决方案2】:

    你也可以把它放在 Form 构造函数的末尾,像这样:

    public function __construct($name = 'user') {
       ...
       $this->getInputFilter()->get('first_name')->setRequired(false);
    }
    

    【讨论】:

      【解决方案3】:

      我认为有像 required 这样的默认验证器。

      你试过了吗:

      ->setAllowEmpty(false)
      

      ->setRequired(false)
      

      来自this other so question

      【讨论】:

      • 感谢您的回答。使用 setRequired 有效,但必须在 InputFilter 上完成,因为这是一个 ZF2 问题。对于表明该问题也与 ZF1 相关的错误和误导性标签,我深表歉意。感谢 akond 删除了不正确的标签。
      【解决方案4】:

      对我来说,上述任何一种方法都不起作用,所以我将发布我对这个问题的答案:

      像这样添加一个验证器:

      @Form\Validator({"name":"NotEmpty", "options":{"type":"string"}})
      

      它适用于“0”和“1”值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-09
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-12
        • 2017-07-09
        • 1970-01-01
        相关资源
        最近更新 更多