Adding an input filter to a set of fields in ZF2

I have a form Registerthat has a set of fields Profile, which in turn has a set of fields Account. The sets of fields implement InputFilterProviderInterfaceand therefore the method getInputFilterSpecification. Here I have added common validators and filters that should be used whenever fields are used.

Now, in my registration form, I want to verify that the account with the given username does not exist yet. Therefore, I need to add a validator to complement the validators that are defined in the set of fields Account. Here I am in trouble. With a little work, I found a way to add input filters to> fields . So, I figured, I could add an additional input filter to my set Account.

class Register extends Zend\InputFilter\InputFilter
{
    public function __construct()
    {
        $this->add(new RegisterProfileFilter(), 'profile');
    }
}

With the above code, I can add an input filter to my field set Profile, and inside this input filter I can do the same for my field set Account. However, there seem to be two problems with this approach:

  • It seems I need to create an input filter for each of my fields in the hierarchy; in this case, I need to create an input filter for the field set Profileso that I can add an input filter to the field set Account, even if I do not need to add any validators or anything to ProfileFIELDSET. This does not work if I try to add a filter to the field Accountdirectly
  • It seems that adding an input filter object to a set of fields destroys the filter that I defined in the field method getInputFilterSpecificationinstead of merging the two as I want

, , ( Zend\InputFilter\InputFilter, fieldset ? , . -, ?

, - .

// The code has been altered to be more self-explanatory and shorter

class RegisterForm extends \Zend\Form\Form
{
    public function __construct()
    {
        parent::__construct('register');

        // Elements are added here

        $profileFieldset = new ProfileFieldset();
        $profileFieldset->setUseAsBaseFieldset(true);
        $this->add($profileFieldset);
    }
}


class ProfileFieldset extends \Zend\Form\Fieldset implements InputFilterProviderInterface
{
    public function __construct()
    {
        parent::__construct('profile');

        // Elements are added here

        $this->add(new AccountFieldset());
    }

    public function getInputFilterSpecification()
    {
        return array(
            /***** The below is apparently cleared when adding an input filter to this fieldset *****/
            'some_element1' => array(
                'required' => false,
            ),
            'some_element2' => array(
                'required' => false,
            ),
        );
    }
}


class AccountFieldset extends \Zend\Form\Fieldset implements InputFilterProviderInterface
{
    public function __construct()
    {
        parent::__construct('account');

        // Elements are added here
    }

    public function getInputFilterSpecification()
    {
        return array(
            /***** This is the element that I want to add an additional validator to in a specific context (form) *****/
            'username' => array(
                'required' => true,
                'validators' => array(
                    new Validator\StringLength(array(
                        'min' => 4,
                        'max' => 15,
                    )),
                    new I18nValidator\Alnum(false),
                ),
            ),

            // Other elements here
        );
    }
}
+4
1

Zend\Form. - .

, , , , InputFilter getInputFilter .

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;
    }
}    

InputFilters Form, Fieldset, Fieldset " " (= Form). , , .

+6

Source: https://habr.com/ru/post/1537390/


All Articles