Php 使用zend framework2的回调验证器进行zend表单验证时无效

Php 使用zend framework2的回调验证器进行zend表单验证时无效,php,zend-framework2,zend-validate,Php,Zend Framework2,Zend Validate,我在验证元素的值数组时遇到问题。我搜索了很多,找到了一个回调函数来验证数据 下面是我正在使用的验证代码,但它不起作用 <?php namespace Tutorials\Form; use Zend\InputFilter\Factory as InputFactory; // <-- Add this import use Zend\InputFilter\InputFilter; // <-- Add this

我在验证元素的值数组时遇到问题。我搜索了很多,找到了一个回调函数来验证数据

下面是我正在使用的验证代码,但它不起作用

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

我注意到的第一件事是,两个回调都缺少要验证的值。这两个签名都是function()use(),通常情况下,为了进行验证,应该是function($itemToBeValidated)use()。您是否试图执行验证范围之外的操作?否。当我将表单字段数据作为数组传递时,我将使用类似函数($formFieldData),然后它将给出未定义的变量$formFieldData。。因此,我使用use($formFieldData)传递了它。
 <?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.