ZF2简单形式验证

时间:2017-02-20 11:12:49

标签: validation zend-framework2 zend-form

我试图在zend框架2中验证一个简单的表单几天了。 我检查了文档和很多考虑这个主题的帖子,但我找不到解决方案!

我有一个非常简单的形式:

class AlimentForm extends Form
{
public function __construct($name = null)
{
    parent::__construct('aliment');

    $this->add(array(
        'required'=>true,
        'name' => 'year',
        'type' => 'Text',
        'options' => array(
            'label' => 'Jahr',
        ),
    ));
    $this->add(array(
        'required'=>true,
        'name' => 'number',
        'type' => 'Text',
        'options' => array(
            'label' => 'Number',
        ),
    ));
    $this->add(array(
        'name' => 'submit',
        'type' => 'Submit',
        'attributes' => array(
            'value' => 'Go',
            'id' => 'submitbutton',
        ),
    ));
}
}

我创建了一个自定义的InputFilter:

namespace Application\Form;

use Zend\InputFilter\InputFilter;
class AlimentInputFilter extends InputFilter {
    public function init()
    {
    $this->add([
        'name'       => AlimentForm::year,
        'required'   => true,
        'validators' => array(
          array(
              'name' => 'Between',
              'options' => array(
                  'min' => 1900,
                  'max' => 3000,
              ),
          ),
        ),
    ]);
}
}

最后在我的控制器中我尝试验证表单

public function alimentAction(){
    $form = new AlimentForm();

    $form->setInputFilter(new AlimentInputFilter());

    $request = $this->getRequest();
    if ($request->isPost()) {
       $form->setData($request->getPost());

       if ($form->isValid()) {
           $year = $form->get('year')->getValue();
           $number = $form->get('number')->getValue();

           return array('result' => array(
               "msg" => "In the Year ".$year." you get ".$number." Points"
           ));
       }
    }
    return array('form' => $form);
}

这不是那么困难,但是从所有那些不同的方式来验证我在网上找到的表格,我有点困惑......

我错过了什么?

提前致以问候和谢意 U.H。

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。

我为具有年份和数字属性的食物表单创建了一个模型 我在该模型中定义了一个输入过滤器:

use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class Aliment implements InputFilterAwareInterface
{
    public $year;
    public $number;

    public function exchangeArray($data){
        $this->year             = (!empty($data['year']))?$data['year']:null;
        $this->number          = (!empty($data['number']))?$data['number']:null;
    }

    public function setInputFilter(InputFilterInterface $inputFilter)
    {
        throw new \Exception("Not used");
    }

    public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();


            $inputFilter->add(array(
                'name'     => 'year',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'Between',
                        'options' => array(
                            'min' => 1900, 
                            'max' => 3000
                        )
                    )
                ),
            ));

            $inputFilter->add(array(
                'name'     => 'number',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'Between',
                        'options' => array(
                            'min' => 1, 
                            'max' => 10
                        )
                    )
                ),
            ));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }

}

在控制器的动作方法中,我能够将模型的InputFilter设置为窗体并瞧!它奏效了!

public function alimentAction(){
    $form = new AlimentForm();

    $request = $this->getRequest();
    if ($request->isPost()) {
        $aliment = new \Application\Model\Aliment;
        $form->setInputFilter($aliment->getInputFilter());
        $form->setData($request->getPost());


        if ($form->isValid()) {
            $aliment->exchangeArray($form->getData());
            $year           = $form->get('year')->getValue();
            return array(
                'result' => array(
                    //your result
                )
            );
        }
    }
    return array('form' => $form);
}

现在正确验证表单并返回相应的错误消息。

我希望,它可以帮助那些在验证方面遇到类似问题的人!