0

I'm trying to create a simple Zend form using pm_Form_Simple class. I need a callback validator with its addElement method. Here's what I did.

$form = new pm_Form_Simple();
$form->addElement('text','my_number',
                 array('label'=>'Your number',
                'validators'=>array(
                                array('name'=>'Callback',false,'
                                options'=>array('messages'=>'Invalid number',
                               'callback'=>function($value,$context=array()){
                                                if($value < 0){ 
                                                     return false;
                                                }else{ 
                                                    return true;
                                                }    
                                         },
                                 ))
                   )));

This gives me an error when submitting the form

Missing argument 1 for CommonPanel_Validate_Callback::__construct()

I have tried this structure also

 array(
      new \Zend\Validator\Callback(
      array(
     'messages' => array(\Zend\Validator\Callback::INVALID_VALUE => 'Invalid number'),
      'callback' => function($value){
                        if($value < 0){ 
                           return false;
                        }else{ 
                          return true;
                        }  
                    }
   )))

This gave me an error include_once(Zend/Validator/Callback.php): failed to open stream: No such file or directory

I don't see Zend/Validator in my zend installation,so I changed it to zend/Validate

This time the error was Class 'Zend\Validate\Callback' not found

I know there exists LessThan & GreaterThan validators, but can anyone kindly help/guide me on how to add callback validator with addElement function ?

Vipin Kumar KM
  • 356
  • 5
  • 17
  • 1
    Somehow you are mixing zf1 and zf2. Zend/Validator is in zf2, Zend_Validate in zf1. Download again zf2 and try again with your second approach. – Ronnie Nov 07 '14 at 12:58
  • @Demo, Sorry I'm new to Zend. But let me ask, is my first method is any close to how it should be done in ZF1? – Vipin Kumar KM Nov 08 '14 at 04:28

0 Answers0