ZF2: How do I use InArray validator to validate Mu

2019-02-24 19:01发布

I have a ZF2 form where I had to disable native validators, for a specific reason.

Then, when adding elements programatically to the form I also add validators.

One of the elements is a Multiselect array.

$form->add( array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
    (
        'label' => 'few items',
        'value_options' => Array
            (
                'one' => 'one',
                'two' => 'two',
                'three' => 'three',
                'four' => 'four',
            )
    ),
'attributes' => array
    (
        'multiple' => 'multiple',
        'value' => array('two','three'),
        'required' => 1,
        'id' => 'few_items'
    ),
'name' => 'few_items'
));

Also, I'm going to add an InArray validator:

if($f instanceof \Zend\Form\Element\Select){
    $inputFilter->add($factory->createInput(array(
        'name'     => $f->getName(),
        'required' => $f->getAttribute('required') == 1,
        'validators' => array(
            array(
                'name'    => 'InArray',
                'options' => array(
                    'haystack' => $f->getValueOptions(),
                    'messages' => array(
                        InArray::NOT_IN_ARRAY => 'Please select an option',
                    ),
                ),
            ),
        ),
    )));
}

The problem is that the validator always fails, because in POST multiselect field will return an array, and actually looking inside the InArray validator, it uses in_array(...) PHP function which is not suitable for this - array_intersect would do the trick, but before writing my own validator I do have a feeling that this wheel was invented already!

Having looked around I see that there was a bug raised to this effect (http://framework.zend.com/issues/browse/ZF2-413), and the solution was to introduce Explode validator, but I'm not sure how to add it into my input filter.

Thanks for your suggestions.

1条回答
手持菜刀,她持情操
2楼-- · 2019-02-24 19:55

Actually, following the bugfix link, I figured out how to do the validation. Explode validator would break down the value and apply a validator to each part:

if($f instanceof \Zend\Form\Element\Select){
    $inputFilter->add($factory->createInput(array(
        'name'     => $f->getName(),
        'required' => $f->getAttribute('required') == 1,
        'validators' => array(
            array(
                'name' => 'Explode',
                'options' => array(
                    'validator' => new InArray(array(
                            'haystack' => $f->getValueOptions(),
                            'valueDelimeter' => null,
                            'messages' => array(
                                InArray::NOT_IN_ARRAY => 'Please select an option',
                            ),
                        ))
                )
            ),
        ),
    )));
}

Leaving this question here, because I haven't found any other answers to this myself and hopefully this will assist people in the future.

查看更多
登录 后发表回答