ZF2: How do I use InArray validator to validate Multiselect form element?

﹥>﹥吖頭↗ 提交于 2019-12-01 21:01:01

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!