【问题标题】:Zend Framework 2 - Writing and setting a good InputFilterZend Framework 2 - 编写和设置一个好的 InputFilter
【发布时间】:2015-06-20 19:14:47
【问题描述】:

我正在 ZF2 站点中创建一个表单,在这里我已经解决了许多问题: Zend Framework 2 - Submitting a form(也可以在此处找到代码)。
现在我有另一个问题:在我的控制器中,form->isValid() 无论如何都会返回 true。我的目标是通过 PHP 进行验证,然后通过 Ajax 告诉用户一切是否正常。我想我的 InputFilter 出了点问题,或者它没有正确附加到我的表单。
有什么建议么?提前致谢。

【问题讨论】:

    标签: php forms validation zend-framework


    【解决方案1】:

    也解决了这个问题。 将所有验证器与表单放在同一个类中就可以了;这是(糟糕的)官方文档和这里和其他地方的一些论坛主题的混合体。表单类现在看起来是这样的:

    <?php
    namespace Site\Form;
    
    use Zend\Form\Form;
    use Zend\Form\Element;
    use Zend\InputFilter\Input;
    use Zend\InputFilter\InputFilter;
    use Zend\Validator;
    
    class ContactForm extends Form {
        public function __construct($name=null, $options=array ()) {
            parent::__construct ($name, $options);
    
            $this->setAttributes(array(
                "action" => "./",
            ));
    
    
            $nameInput = new Element\Text("nome");
            $nameInput->setAttributes(array(
                "placeholder" => "Nome e cognome",
                "tabindex" => "1"
            ));
    
            $this->add($nameInput);
    
            $emailInput = new Element\Text("email");
            $emailInput->setAttributes(array(
                "placeholder" => "Indirizzo e-mail",
                "tabindex" => "2"
            ));
    
            $this->add($emailInput);
    
            $phoneInput = new Element\Text("phone");
            $phoneInput->setAttributes(array(
                "placeholder" => "Numero di telefono",
                "tabindex" => "3",
            ));
    
            $this->add($phoneInput);
    
            $messageArea = new Element\Textarea("messaggio");
            $messageArea->setAttributes(array(
                "placeholder" => "Scrivi il tuo messaggio",
                "tabindex" => "4"
            ));
    
            $this->add($messageArea);
    
            $submitButton = new Element\Button("submit");
            $submitButton
                ->setLabel("Invia messaggio")
                ->setAttributes(array(
                    "type" => "submit"
                ));
    
            $this->add($submitButton);
    
            $resetButton = new Element\Button("reset");
            $resetButton
            ->setLabel("Cancella")
            ->setAttributes(array(
                    "type" => "reset"
            ));
    
            $this->add($resetButton);
    
            $inputFilter = new InputFilter();
    
            $nome = new Input("nome");
            $nome->getValidatorChain()
            ->attach(new Validator\StringLength(3));
    
            $email = new Input("email");
            $email->getValidatorChain()
            ->attach(new Validator\EmailAddress());
    
            $phone = new Input("phone");
            $phone->getValidatorChain()
            ->attach(new Validator\Digits());
    
            $message = new Input("messaggio");
            $message->getValidatorChain()
            ->attach(new Validator\StringLength(10));
    
            $inputFilter->add($nome)
                        ->add($email)
                        ->add($phone)
                        ->add($message);
    
            $this->setInputFilter($inputFilter);
        }
    }
    ?>
    

    我稍后会尝试工厂,但现在,这可行。

    【讨论】:

      猜你喜欢
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多