0
votes

I'm new to zend framework. I created a form where i have 2 text field. Now, i want to validate that one of them is not empty and if so the form is valid. How can i set a validation like this in an InputFilter?

Any help is greatly appreciated.

1

1 Answers

0
votes

To set the validator in an input filter you would do something like this in a validator class:

$inputFilter->add($factory->createInput(array(
    'name'     => 'foo_bar_input',
    'required' => true,
    'filters'  => array(
        array('name' => 'StripTags'),
        array('name' => 'StringTrim')
    ),

    // add your validators here         
    'validators' => array(
        // check to see if an input is empty
        array(
            'name'    => 'Zend\Validator\NotEmpty',
            'options' => array(),
        )
    )
)));

Then to use the filter with your form, do something like:

$validate = $this->getServiceLocator()->get('your_validator_class');
$form->setInputFilter($validate->getInputFilter());

The just check to see if your form is valid:

if ($form->isValid()) 
{ 
    // do stuff
}

Here is ZF2's documentation on the NotEmpty validator: http://framework.zend.com/manual/2.0/en/modules/zend.validator.set.html#notempty