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 ? , . -, ?
, - .
class RegisterForm extends \Zend\Form\Form
{
public function __construct()
{
parent::__construct('register');
$profileFieldset = new ProfileFieldset();
$profileFieldset->setUseAsBaseFieldset(true);
$this->add($profileFieldset);
}
}
class ProfileFieldset extends \Zend\Form\Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('profile');
$this->add(new AccountFieldset());
}
public function getInputFilterSpecification()
{
return array(
'some_element1' => array(
'required' => false,
),
'some_element2' => array(
'required' => false,
),
);
}
}
class AccountFieldset extends \Zend\Form\Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('account');
}
public function getInputFilterSpecification()
{
return array(
'username' => array(
'required' => true,
'validators' => array(
new Validator\StringLength(array(
'min' => 4,
'max' => 15,
)),
new I18nValidator\Alnum(false),
),
),
);
}
}