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

后端 未结 1 681
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 12:00

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.

1条回答
  •  北荒
    北荒 (楼主)
    2021-01-20 12:20

    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.

    0 讨论(0)
提交回复
热议问题