8

My model object implements InputFilterAwareInterface and have getInputFilter() method, that returned Zend\InputFilter\InputFilter instance

I don't need form in my case, I just need validate elements from array. How I can use Zend\InputFilter\InputFilter for validating data from array without creating form class?

1 Answer 1

17

Hope the code is self-explanatory (setData to set your array, setValidationGroup to check all elements, and isValid to get result of validation):

use \Zend\InputFilter\InputFilterInterface;

/* ... */

/** @var $data array */

/** @var $filter InputFilterInterface */
$filter = $this->getInputFilter();

$isValid = $filter->setData($data)
                  ->setValidationGroup(InputFilterInterface::VALIDATE_ALL)
                  ->isValid();

if (!$isValid)
{
    $errorMessages = $filter->getMessages();
    /* ... */
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you suggest an approach to validate only one row from set?
@yurisnk You can set list of fields to validate in call to setValidationGroup(), just use ->setValidationGroup('field_name') for single field or ->setValidationGroup(array('field_name1', 'field_name2')) for set of fields in above example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.