Derrick
Derrick

Reputation: 336

Zend Framework 2 Setting Zend\InputFilter setRequired()

I have a Form\Element with an InputFilter whose 'required' value is set to "false". Under certain circumstances that Element will need the 'required' value set to "true" from within the Controller.

When I set Zend\InputFilter setRequired(true) in my Controller, it doesn't seem to be respected when the $form->isValid() method is called. However, if the filter is set where 'required' is "true" in the Zend\InputFilter (and not dynamically set inside the Controller) then it works as expected - but that's not my desired solution as I use this form and filter in several locations and sometimes the field is required and other times it is not.

In my Controller, I have the following:

$form = new UserDataForm();
$request = $this->getRequest();

if ($request->isPost()) {
    $update = new UserFilter();
    // The following doesn't seem to be respected
    $update->getInputFilter()->get('userName')->setRequired(true);

    $form->setInputFilter($update->getInputFilter());
    $form->setData($request->getPost());

    if($form->isValid()) {
        //The result is true even when the 'userName' var is not set in the POST data.
        echo("Is Valid");
    } else {
        echo($form->getMessages());
    }

I have set my UserDataForm() class like so:

class UserDataForm extends Form
{
public function __construct($name = null, array $userTypes) {
    parent::__construct('user');
    $this->setAttribute('method','post');

    $this->add(array(
        'name' => 'userName',
        'attributes' => array(
            'type' => 'text',
            'class'=> 'small'
        ),
        'options' => array(
            'label' => 'Username:'
        )
    ));

    //... and so on...

I have set my UserFilter class like so:

public function getInputFilter() {
    if (!$this->inputFilter) {
        $inputFilter = new InputFilter();
        $factory     = new InputFactory();

        $inputFilter->add($factory->createInput(array(
            'name'      => 'userName',
            'required'  => false,
            'filters'   => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name'  => 'StringLength',
                    'options'  => array(
                        'encoding'  => 'UTF-8',
                    )
                ),
            )
        )));

    // ...

Can someone explain why the $update->getInputFilter()->get('userName')->setRequired(true) as called from my Controller doesn't seem to be respected when the form is validated?

Upvotes: 1

Views: 7583

Answers (1)

Derrick
Derrick

Reputation: 336

I found the problem after resting on it for a while. Apparently, ->setRequired(true) is mutually exclusive when assigned dynamically. If you are looking for the same behavior as setting 'required' => true from the \FilterInput, then you also need to add ->setAllowEmpty(false) in addition.

So my revised code now looks like this:

$update->getInputFilter()->get('userName')->setRequired(true);
$update->getInputFilter()->get('userName')->setAllowEmpty(false);

Derrick

Upvotes: 8

Related Questions