简体   繁体   English

Zend Framework 2-编写和设置良好的InputFilter

[英]Zend Framework 2 - Writing and setting a good InputFilter

I'm creating a form in a ZF2 site, where i already solved many problems here: Zend Framework 2 - Submitting a form (see there to find the code as well). 我正在ZF2网站上创建表单,在这里我已经解决了许多问题: Zend Framework 2-提交表单 (也可以在此处查找代码)。
Now i have another problem: in my controller, form->isValid() returns true no matter what. 现在我有另一个问题:在我的控制器中,无论如何, form->isValid()返回true。 My goal is to have a validation through PHP, then tell the user if everything is good or not via Ajax. 我的目标是通过PHP进行验证,然后通过Ajax告诉用户是否一切正常。 I suppose something went wrong with my InputFilter or it's not properly attached to my form. 我想我的InputFilter出了点问题,或者未正确附加到我的表单上。
Any suggestions? 有什么建议么? Thanks in advance. 提前致谢。

Solved this one as well. 也解决了这一问题。 Putting all validators in the same class as the form did the job; 将所有验证者放在与表格相同的类别中; this was a mix of (poor) official docs and some forum topics here and somewhere else. 这是(差的)官方文档以及此处以及其他地方的一些论坛主题的混合体。 Form class now looks this way: 表单类现在看起来是这样的:

<?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);
    }
}
?>

I'll try factories later, but for now, this works. 稍后我将尝试工厂,但是现在可以了。

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

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