0

I have a problem in validating the array of value of an element. I search a lot find a callback function to validate that data.

Below is the validation code which i am using but it is not working

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

}

In the above line of code the value of $data['data']['topic_name']=array('0' => 1,'1' => 1)

and $data['data']['sub_topic_name']=array('0'=>'Testing','1'=>'Testing1');

and i am calling it $form = new AddSubTopicForm(); $logFilterForm = new AddSubTopicFilterForm(); $form->setInputFilter($logFilterForm->getInputFilter()); $form->setData($request->getPost());

Below is the of the form class

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

But it is not calling the filter callback function rather give me an error on first first form field that 'value is required ,can't be empty'

I don't why it is not validating data.Suggest me what where i am wrong and how can i overcome from this problem. Any help would be appreciated. Than.x

Azhar Ahmad
  • 183
  • 12
  • the first thing I notice without looking too deeply is that both your callbacks are missing the value to be validated. Both your signatures are function() use (), when usually, for validation it should be function($itemToBeValidated) use (). Are you trying to do something outside the scope of validation perhaps? – Tim Klever Mar 09 '16 at 17:38
  • No. when i pass form field data as an array then like function($formFieldData) and i will use it then it give undefined variable $formFieldData.. So i passed that by using use($formFieldData). – Azhar Ahmad Mar 11 '16 at 05:36

0 Answers0