使用zend framework2

时间:2016-03-07 12:55:47

标签: php zend-framework2 zend-validate

我在验证元素的值数组时遇到问题。我经常搜索一个回调函数来验证这些数据。

以下是我正在使用的验证码,但它无效

  <?php
   namespace Tutorials\Form;

   use Zend\InputFilter\Factory as InputFactory;     // <-- Add this import
   use Zend\InputFilter\InputFilter;                 // <-- Add this import
   use Zend\InputFilter\InputFilterAwareInterface;   // <-- Add this import
   use Zend\InputFilter\InputFilterInterface;  
   use Zend\Validator\Callback;
   use Zend\I18n\Validator\Alpha;


  class AddSubTopicFilterForm extends InputFilter implements InputFilterAwareInterface  {
     protected $inputFilter;    
     public $topicData;
     public $subTopicData;

     function __construct($data = array()) {

        $articles = new \Zend\InputFilter\CollectionInputFilter();
        $articlesInputFilter = new \Zend\InputFilter\InputFilter();
        $articles->setInputFilter($articlesInputFilter);

        $this->add(new \Zend\InputFilter\Input('title'));        
        $this->add($articles, 'articles');
        if(!empty($data['data']['topic_name'])) {
           $this->topicData = $data['data']['topic_name'];
        }
       if(!empty($data['data']['sub_topic_name'])) {
         $this->subTopicData = $data['data']['sub_topic_name'];
       }
    }
    public function setInputFilter(InputFilterInterface $inputFilter){
      throw new \Exception("Not used");
    }

    public function getInputFilter(){
      if (!$this->inputFilter) {
         $dataTopic = $this->topicData;
         $dataSubTopic = $this->subTopicData;
         $inputFilter = new InputFilter();
         $factory     = new InputFactory();

        $inputFilter->add($factory->createInput(array(
            'name'     => 'topic_name',
            'required' => true,
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'Callback',
                    'options' => array(
                        'messages' => array(
                            \Zend\Validator\Callback::INVALID_VALUE => 'Seletec value is not valid',
                        ),
                        "callback" => function() use ($dataTopic) {
                              $strip = new \Zend\I18n\Validator\IsInt();
                              foreach($dataTopic as $key => $tag) {
                                $tag = $strip->isValid((int)$tag);
                                $dataTopic[$key] = $tag;
                              }
                              return $dataTopic;
                        },
                    ),
                ),
            ),

        )));
        $inputFilter->add($factory->createInput(array(
                'name'     => 'sub_topic_name',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'Callback',
                        'options' => array(
                            'messages' => array(
                                \Zend\Validator\Callback::INVALID_VALUE => 'Invalid Sub Topic Name',
                            ),
                            "callback" => function() use ($dataSubTopic) {
                                  $strip = new \Zend\Validator\StringLength(array('encoding' => 'UTF-8','min' => 1,'max' => 100));
                                  foreach($dataSubTopic as $key => $tag) {
                                    $tag = $strip->isValid($tag);
                                    $dataSubTopic[$key] = $tag;
                                  }
                                  return $dataSubTopic;
                            },
                        ),
                    ),
                ),

            )));
       $this->inputFilter = $inputFilter;
    }

    return $this->inputFilter;
}

}

在上面的代码行中的值           $ data ['data'] ['topic_name'] =数组('0'=&gt; 1,'1'=&gt; 1)

和$ data ['data'] ['sub_topic_name'] = array('0'=&gt;'Testing','1'=&gt;'Testing1');

我打电话给它            $ form = new AddSubTopicForm();            $ logFilterForm = new AddSubTopicFilterForm();            $形式 - &GT; setInputFilter($ logFilterForm-&GT; getInputFilter());            $形式 - &GT;使用setData($请求 - &GT;的getPost());

以下是表单类

 <?php

   namespace Tutorials\Form;


   use Zend\Form\Element;
   use Zend\Form\Form;


   class AddSubTopicForm extends Form {

      public function __construct($data = array()){ 

      parent::__construct('AddSubTopic');
      $this->setAttribute('class', 'form-horizontal');
      $this->setAttribute('novalidate', 'novalidate');

        $this->add(array(     
                    'type' => 'Zend\Form\Element\Select',       
                    'name' => 'topic_name[]',

                    'attributes' =>  array(
                        'id' => 'topic_name', 
                        'class' => 'form-control',
                        ),

                    'options' => array(
                        'label' => ' Topic Name',
                        'empty_option'=>'---None--- ',
                        'value_options' => array(
                                 '1' => 'PHP'
                                   ),
                    ),
        ));

        $this->add(array(
                'name' => 'sub_topic_name[]',
                'attributes' => array(
                    'type'  => 'text',
                    'id' => 'sub_topic_name',
                    'class' => 'form-control',
                    'value' => ''
                ),
                'options' => array(
                    'label' => ' Sub Topic Name',
                ),
            ));

        $this->add(array(
            'name' => 'id',
            'attributes' => array(
                'type'  => 'hidden',
                'id' => 'id'
            )
        ));

         $button = new Element('add_more_sub_topic');
         $button->setValue('+AddMore');
         $button->setAttributes(array(
                 'type'  => 'button',
                 'id'=>'add_more',
                 'class'=>'btn btn-info'
             ));

         $save = new Element('save');
         $save->setValue('Save');
         $save->setAttributes(array(
                 'type'  => 'submit',
                 'id'=>'save',
                 'class'=>'btn btn-info'
             ));


         $reset = new Element('reset');
         $reset->setValue('Reset');
         $reset->setAttributes(array(
                 'type'  => 'reset',
                 'id'=>'reset',
                 'class'=>'btn'
             ));

        $this->add($button);
        $this->add($save);
        $this->add($reset);
    }//end of function_construct.
 }//end of registration form class.

但是它没有调用过滤器回调函数而是在第一个表单字段上给出了一个错误,即“值是必需的,不能为空”

我不知道为什么它没有验证数据。请告诉我我错在哪里以及如何克服这个问题。任何帮助,将不胜感激。 Than.x

0 个答案:

没有答案