在ZendFramework2表单中设置验证器

时间:2014-02-04 14:47:25

标签: php forms validation zend-framework

我正在尝试在ZF2中编写我的第一个表单,而我的代码是

                namespace Frontend\Forms;
            use Zend\Form\Form;
            use Zend\Validator;

            class Pagecontent extends Form
            {
                public function __construct($name = null)
                {
                    // we want to ignore the name passed
                    parent::__construct('logo');
                    $this->setAttribute('method', 'post');      
                    $this->add(array(
                        'name' => 'content_yes_no',
                        'type'=>'text',
                        'required' => true,
                        'validators' => array(
                                'name' => 'Alnum',
                                'options'=>array(
                                    'allowWhiteSpace'=>true,
                                ),
                        ),
                    ));
                }
            }

我想知道我可以设置这样的验证器吗? 请建议

3 个答案:

答案 0 :(得分:2)

你必须用另一个数组包围验证器:

'validators' => array(
        array(
                        'name' => 'Alnum',
                        'options' => array(
                                'allowWhiteSpace'=>true,
                        ),
        ),
),

答案 1 :(得分:1)

您可以使用输入过滤器组件:

<?php
namespace Frontend\Forms;

use Zend\Form\Form;
use Zend\Validator;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;

class Pagecontent extends Form
{

    public function __construct($name = null)
    {
        ...
        $inputFilter = new InputFilter();
        $factory     = new InputFactory();
        $inputFilter->add($factory->createInput(array(
            'name'     => 'content_yes_no',
            'required' => true,
            'filters'  => array(),
            'validators' => array(
                array(
                    'name' => 'Alnum',
                    'options' => array(
                        'allowWhiteSpace' => true,
                    ),
                ),
            ),
        )));

        $this->setInputFilter($inputFilter);
    }
}

//你的控制器

$form = new \Frontend\Forms\Pagecontent();
$form->setData($request->getPost());

if ($form->isValid()) {
    // your code
}

答案 2 :(得分:1)

要设置过滤器和验证器,您需要一个inputFilter。通常,您将找到在表单类或关联的模型类中定义的inputFilter。这是表单的模板。

<?php
/* All bracket enclosed items are to be replaced with information from your 
 * implementation. 
 */
namespace {Module}\Form;

class {Entity}Form
{
    public function __construct()
    {
        // Name the form
        parent::__construct('{entity}_form');

        // Typically there is an id field on the form
        $this->add(array(
            'name' => 'id',
            'type' => 'Hidden',
        ));

        // Add a csrf field to help with security
        $this->add(array(
            'type' => 'Zend\Form\Element\Csrf',
            'name' => 'csrf'
        ));

        // Add more form fields here
        $this->add(array(
            'name' => 'example',
            'type' => 'Text',
            'options' => array(
                'label' => 'Example',
            ),
        ));

        //Of course we need a submit button
        $this->add(array(
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => array(
                'value' => 'Submit',
                'id' => 'submitbutton',
            ),
        ));
    }
}

表单定义了将在表单中显示的所有元素。现在,您可以在表单类中创建inputFilter,也可以在与表单类关联的模型中创建inputFilter。无论哪种方式,它看起来像:

<?php
/* All bracket enclosed items are to be replaced with information from your 
 * implementation. 
 */
namespace {Module}\Model;

/*
 * Include these if you require input filtering. 
 */
use Zend\InputFilter\Factory as InputFactory;   
use Zend\InputFilter\InputFilter;                
use Zend\InputFilter\InputFilterAwareInterface;  
use Zend\InputFilter\InputFilterInterface;   

class {Model} implements InputFilterAwareInterface 
{
    /*
     * Add in model members as necessary
    */
    public $id;
    public $example;

    /*
     * Declare an inputFilter
     */
    private $inputFilter;

    /*
     * You don't need a set function but the InputFilterAwareInterface makes
     * you declare one
     */
    public function setInputFilter(InputFilterInterface $inputFilter)
    {
        throw new \Exception("Not used");
    }

    /*
     * Put all of your form's fields' filters and validators in here
     */
    public function getInputFilter()
    {
        if (!$this->inputFilter) 
        {
            $inputFilter = new InputFilter();
            $factory     = new InputFactory();

            $inputFilter->add($factory->createInput(array(
                'name'     => 'id',
                'required' => true,
                'filters'  => array(
                    array('name' => 'Int'),
                ),
            )));

            // This example input cannot have html tags in it, is trimmed, and 
            // must be 1-32 characters long
            $inputFilter->add($factory->createInput(array(
                'name'     => 'example',
                'required' => false,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 32,
                        ),
                    ),
                ),
            )));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }
}

然后,当您编写控制器的动作时,您可以将它们全部组合在一起:

if($request->isPost())
{
    $model = new Model;
    $form->setInputFilter($model->getInputFilter());
    $form->setData($request->getPost());

    if ($form->isValid()) 
    {
        // Do some database stuff
    }
}

请注意,我们从模型中获取inputFilter并使用表单setInputFilter()方法附加它。

总而言之,您必须创建一个表单类来放置所有表单元素,然后创建一个inputFilter来保存所有过滤器和验证器。然后,您可以在控制器中获取inputFilter并将其应用于表单。当然,这只是为猫皮做几个方法。