【问题标题】:Set validators in form ZendFramework2以 ZendFramework2 的形式设置验证器
【发布时间】:2014-02-04 14:47:25
【问题描述】:

我正在尝试在 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,
                                ),
                        ),
                    ));
                }
            }

我想知道我可以这样设置验证器吗? 请指教

【问题讨论】:

  • 问之前有没有试过?
  • 是的,但它不起作用:(

标签: php forms validation zend-framework


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    要设置过滤器和验证器,您需要一个 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。不管怎样,它看起来像:

    <?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 并将其应用于表单。当然,这只是给猫剥皮的几种方法。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-29
        • 2020-09-27
        • 1970-01-01
        • 2018-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多