Conditionally required in Zend Framework's 2 InputFilter
Asked Answered
S

4

6

I am making an application using Zend Framework 2. I am validating input using it's InputFilter. Is it possible, to make some Inputs required conditionally? I mean I have code like that:

$filter = new \Zend\InputFilter\InputFilter();
$factory = new \Zend\InputFilter\Factory();
$filter->add($factory->createInput(array(
    'name' => 'type',
    'required' => true
)));
$filter->add($factory->createInput(array(
    'name' => 'smth',
    'required' => true
)));

I want the field something, to be required, ONLY when type is equal 1. Is there a built-in way to do that? Or should I just create custom validator?

Sickroom answered 17/11, 2012 at 17:37 Comment(0)
E
8

First of all, you may want to enable validation on empty/null values as of Empty values passed to Zend framework 2 validators

You can use a callback input filter as in following example:

$filter = new \Zend\InputFilter\InputFilter();
$type   = new \Zend\InputFilter\Input('type');
$smth   = new \Zend\InputFilter\Input('smth');

$smth
    ->getValidatorChain()
    ->attach(new \Zend\Validator\NotEmpty(\Zend\Validator\NotEmpty::NULL))
    ->attach(new \Zend\Validator\Callback(function ($value) use ($type) {
        return $value || (1 != $type->getValue());
    }));

$filter->add($type);
$filter->add($smth);

This will basically work when the value smth is an empty string and the value for type is not 1. If the value for type is 1, then smth has to be different from an empty string.

Equation answered 27/2, 2013 at 11:31 Comment(1)
Note that due to PHP's very loose way of checking interfaces, it is also possible to define a validator with a method isValid($value, array $context = []). That way, $context can be used to enable/disable validation contextually.Equation
P
3

I couldn't quite get the example by Ocramius to work, as $type->getValue was always NULL. I changed the code slightly to use $context and this did the trick for me:

$filter = new \Zend\InputFilter\InputFilter();
$type   = new \Zend\InputFilter\Input('type');
$smth   = new \Zend\InputFilter\Input('smth');

$smth
    ->getValidatorChain()
    ->attach(new \Zend\Validator\NotEmpty(\Zend\Validator\NotEmpty::NULL))
    ->attach(new \Zend\Validator\Callback(function ($value, $context){
        return $value || (1 != $context['type']);
    }));

$filter->add($type);
$filter->add($smth);
Psychochemical answered 31/5, 2013 at 9:30 Comment(0)
H
0

You can also use setValidationGroup for this.

Create your own InputFilter class where you set validation groups depending on the data that is set inside the inputfilter before executing the actual validation.

class MyInputFilter extends InputFilter
{
   setData($data){
       if(isset($data['type']) && $data['type'] === 1){
           // if we have type in data and value equals 1 we validate all fields including something
           setValidationGroup(InputFilterInterface::VALIDATE_ALL);
       }else{
           // in all other cases we only validate type field
           setValidationGroup(['type']);
       }
       parent::setData($data);
   }
}

This is just a simple example to show what is possible with setValidatioGroup, you can create your own combinations for setting validation groups after your specific needs.

Hild answered 23/4, 2019 at 15:50 Comment(0)
C
-3

Unfortunately you'd have to set the required option based on your conditions like so:

$filter->add($factory->createInput(array(
    'name' => 'smth',
    'required' => (isset($_POST['type']) && $_POST['type'] == '1'),
)));
Cassiecassil answered 19/12, 2012 at 0:21 Comment(1)
This is wrong. You are hereby setting values regarding a filter based on the request that has to be filtered: it's conceptually wrong.Equation

© 2022 - 2024 — McMap. All rights reserved.