Adding input filter to fieldset in ZF2

走远了吗. 提交于 2019-12-05 12:37:57

Merging InputFilters isn't very well covered in the Zend\Form component. Someone must really refactor the whole thing.

Anyway, what will work and therefore I would recommend in your situation, is adding the validator after the whole InputFilter has been created by overriding the getInputFilter method in your Form.

class RegisterForm extends \Zend\Form\Form
{
    public function __construct()
    {
        // add stuff
    }

    public function getInputFilter()
    {
        $formInputFilter = parent::getInputFilter();
        $usernameInput = $formInputFilter->get('profile')->get('account')->get('username');

        $myValidator = new Validator\SomeValidator();
        $usernameInput->getValidatorChain()->addValidator($myValidator);

        return $formInputFilter;
    }
}    

As a sidenote I would recommend defining InputFilters per Form basis and not per Fieldset, since elements in a Fieldset often have different "validation requirements" in different contexts(=Form). But maybe that's more a personal preference.

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