0

I am trying to figure out why Zend Form is not returning errors to my form.

When I take a look inside the: Zdendframework/library/form/form.php file I can see the errors are being generated:

public function isValid()
    {
        if ($this->hasValidated) {
            return $this->isValid;
        }

       ...


        if (!$result) {
            $this->setMessages($filter->getMessages());
        }

        return $result;
    }

If I var_dump($filter->getMessages(), I see the errors.

However when I go back to my form and attempt to dump the messages, there are none available.

My Controller

        $prg = $this->prg();

    if ($prg instanceof Response) {

        return $prg;

    } elseif ($prg === false) {

        return $this->getVM()->setVariables([
            'form' => $this->registerForm
        ]);

    }

    $this->registerForm->setData($prg);


    if ( ! $this->registerForm->isValid($prg) ) {

        return new ViewModel(['form' => $this->updateForm]);
    }

My form:

public function __construct(
        InputFilterInterface $inputFilter,
        $name = null,
        $options = array()
    ) {
        parent::__construct('register', $options);
        $this->register = $inputFilter;
    }

    public function init()
    {
        $this->add(
            [
                'name' => 'csrfcheck',
                'type' => 'csrf'
            ]
        );

        $this->add(
            [
                'name' => 'user',
                'type' => UserFieldset::class,
                'options' => [
                    'use_as_base_fieldset' => true
                ]
            ]
        );

        $this->add(
            [
                'name'       => 'submit',
                'type'       => 'submit',
                'attributes' => [
                    'value' => 'Update User',
                    'class' => 'btn green',
                ]
            ]
        );

        $this->getInputFilter()->add($this->register, 'user');

        $this->setValidationGroup(
            [
                'csrfcheck',
                'user' => [
                    'id',
                    'email',
                    'firstName',
                    'lastName',
                    'roles',
                    'state'
                ]
            ]
        );
    }

My FieldSet

public function __construct(
        ObjectManager $objectManager,
        User $userPrototype,
        $name = null,
        $options = array()
    ) {
        parent::__construct($name, $options);

        $this->objectManager = $objectManager;

        $this->setHydrator(new DoctrineObject($objectManager));
        $this->setObject($userPrototype);

    }

    public function init()
    {

        $this->add(
            [
                'name'       => 'id',
                'type'       => 'hidden',
            ]
        );

        $this->add(
            [
                'name'       => 'uuid',
                'type'       => 'hidden',
            ]
        );

        $this->add(
            [
                'name'       => 'email',
                'type'       => 'email',
                'options'    => [
                    'label' => 'E-Mail',
                    'instructions' => 'Your email address',
                ],
                'attributes' => [
                    'class' => 'form-control input-large'
                ]
            ]
        );

        $this->add(
            [
                'name'       => 'firstName',
                'type'       => 'text',
                'options'    => [
                    'label' => 'Firstname',
                    'instructions' => 'Alphanumeric characters (A,b,c,d,e..)',
                ],

                'attributes' => [
                    'class' => 'form-control',
                    'type' => 'text',
                    'pattern'   => "^[a-zA-Z-]{2,128}$",
                ]
            ]
        );

        $this->add(
            [
                'name'       => 'lastName',
                'type'       => 'text',
                'options'    => [
                    'label' => 'Last name',
                    'instructions' => 'Alphanumeric characters, optional ( \' )',
                ],
                'attributes' => [
                    'class' => 'form-control',
                    'pattern'   => "^[a-zA-Z'-]{2,128}$",
                ]
            ]
        );


        $this->add(
            [
                'name'       => 'state',
                'type'       => 'select',
                'options'    => [
                    'label' => 'User State',
                    'value_options' => [
                        0 => 'Active',
                        1 => 'Locked',
                        2 => 'Deleted'
                    ]
                ],
                'attributes' => [
                    'class' => 'form-control'
                ]
            ]
        );

        //@TODO Multiple set to true to make this work remove attributes if this breaks other forms
        $this->add(
            [
                'name'    => 'roles',
                'type'    => 'DoctrineModule\Form\Element\ObjectSelect',
                'options' => [
                    'object_manager' => $this->objectManager,
                    'target_class'   => HierarchicalRole::class,
                    'property'       => 'name',
                    'find_method'    => [
                        'name' => 'getAccessibleRoles'
                    ],
                    'label' => 'Role'
                ],
                'attributes' => [
                    'multiple' => true,
                ]
            ]
        );

My Filter

function __construct(
        ObjectManager $objectManager,
        ObjectRepository $objectRepository
    ) {
        $this->add(
            [
                'name'     => 'id',
                'required' => true,
                'filters'  => [
                    ['name' => 'Int']
                ]
            ]
        );

        $this->add(
            [
                'name'     => 'uuid',
                'required' => false
            ]
        );


        $this->add(
            [
                'name'       => 'firstName',
                'required'   => true,
                'filters'    => [
                    ['name' => 'StringTrim']
                ],
                'validators' => [
                    [
                        'name'    => 'StringLength',
                        'options' => [
                            'min' => 1,
                            'max' => 128
                        ]
                    ]
                ]
            ]
        );

        $this->add(
            [
                'name'       => 'lastName',
                'required'   => false,
                'filters'    => [
                    ['name' => 'StringTrim']
                ],
                'validators' => [
                    [
                        'name'    => 'StringLength',
                        'options' => [
                            'min' => 1,
                            'max' => 128
                        ]
                    ]
                ]
            ]
        );

        $this->add(
            [
                'name'       => 'email',
                'required'   => false,
                'filters'    => [
                    ['name' => 'StringTrim']
                ],
                'validators' => [
                    [
                        'name'    => 'StringLength',
                        'options' => [
                            'min' => 3,
                            'max' => 255
                        ]
                    ],
                    [
                        'name' => 'EmailAddress',
                        'options' => [
                            'useDomainCheck' => true,  //Set to false in order to validate local developer test mails: myemail.dev
                            'message' => "This is not a valid email address"
                        ],

                    ]
                ]
            ]
        );



        $this->add(
            [
                'name'       => 'password',
                'required'   => false,
                'filters'    => [
                    ['name' => 'StringTrim']
                ],
                'validators' => [
                    [
                        'name'    => 'StringLength',
                        'options' => [
                            'max' => 128
                        ]
                    ]
                ]
            ]
        );

        $this->add(
            [
                'name'       => 'passwordRepeat',
                'required'   => false,
                'filters'    => [
                    ['name' => 'StringTrim']
                ],
                'validators' => [
                    [
                        'name'    => 'StringLength',
                        'options' => [
                            'max' => 128
                        ]
                    ]
                ]
            ]
        );

        $this->add(
            [
                'name'       => 'roles',
                'required'   => false,
                'validators' => [

                ]
            ]
        );

        $this->add( array(
                'name'      => 'state',
                'required'  => false
            ));


    }

And my View

<div class="portlet-body form">
            <!-- BEGIN FORM-->
            <div class="portlet-body form">
                <?php
                $form = $this->form;
                $form->setAttribute('action', $this->url());
                $form->setAttribute('class', 'form-horizontal form-bordered');
                $form->get('user')->get('roles')->setAttribute('class','form-control input-small select2me');
                $form->get('submit')->setValue('Register new user');
                $form->get('submit')->setAttribute('class','btn green');
                $form->prepare();

                $csrf = $form->get('csrfcheck');
                $user = $form->get('user');
                $id = $user->get('id');
                $state = $user->get('state');
                $email = $user->get('email');
                $firstname = $user->get('firstName');
                $lastname = $user->get('lastName');
                $role = $user->get('roles');
                $sub  = $form->get('submit');

                ?>

                <?= $this->form()->openTag($form); ?>
                <?= $this->formElement($csrf); ?>
                <?= $this->formElement($id); ?>
                <?= $this->formElement($state); ?>

                <div class='form-body'>
                    <div class="form-group">
                        <label class="control-label col-md-3"><?= $this->formLabel($email); ?></label>
                        <div class="col-md-5">
                            <div class="input-group" style="text-align:left">
                                <?php echo '<div class="form-group">'.$this->formElement($email).'</div>'; ?>
                            </div>
                            <?php if (!empty($this->formElementErrors($email))) echo '<div class="alert alert-danger">'.$this->formElementErrors($email).'</div>';?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-3"><?= $this->formLabel($firstname); ?></label>
                        <div class="col-md-5">
                            <div class="input-group" style="text-align:left">
                                <?php echo '<div class="form-group">'.$this->formElement($firstname).'</div>'; ?>
                            </div>
                            <?php echo '<div class="alert alert-danger">'.$this->formElementErrors($firstname).'</div>';  ?>

                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-3"><?= $this->formLabel($lastname); ?></label>
                        <div class="col-md-5">
                            <div class="input-group" style="text-align:left">
                                <?php echo '<div class="form-group">'.$this->formElement($lastname).'</div>'; ?>
                            </div>
                            <?php if (!empty($this->formElementErrors($lastname))) echo '<div class="alert alert-danger">'.$this->formElementErrors($lastname).'</div>';?>

                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-3"><?= $this->formLabel($role); ?></label>
                        <div class="col-md-5">
                            <div class="input-group" style="text-align:left">
                                <?php echo '<div class="form-group input-large">'.$this->formElement($role).'</div>'; ?>
                            </div>
                            <?php if (!empty($this->formElementErrors($role))) echo '<div class="alert alert-danger">'.$this->formElementErrors($role).'</div>';?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-3"><?= $this->formLabel($state); ?></label>
                        <div class="col-md-5">
                            <div class="input-group" style="text-align:left">
                                <?php echo '<div class="form-group input-large">'.$this->formElement($state).'</div>'; ?>
                            </div>
                            <?php if (!empty($this->formElementErrors($state))) echo '<div class="alert alert-danger">'.$this->formElementErrors($state).'</div>';?>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="control-label col-md-3"></label>
                        <div class="col-md-5">
                            <div class="input-group" style="text-align:left">
                                <?= $this->formElement($sub); ?>
                            </div>
                        </div>
                    </div>

                </div>
                <?= $this->form()->closeTag(); ?>

EDIT

In the controller if I dump this:

    $result = $this->registerForm->getInputFilter()->getMessages();

    die(var_dump($result));

This outputs:

array (size=1) 'user' => array (size=1) 'firstName' => array (size=1) 'isEmpty' => string 'Value is required and can't be empty' (length=36)

3
  • 1
    Once you have called isValid() if you then dump $this->registerForm->getInputFilter()->getMessages() what do you see? Commented May 21, 2015 at 7:56
  • I see the error message when I dump it... I have added it to the opening post. Commented May 21, 2015 at 8:42
  • Found the error. Its in the controller, I am returning the wrong form on failure. If you add that as your answer, I will give it to you since your suggestion lead me to it. Commented May 21, 2015 at 9:15

1 Answer 1

1

As deduced from the comments and HappyCoder themselves, the problem lies in the Controller where the wrong form is being returned to the view.

if ( ! $this->registerForm->isValid($prg) ) {
    return new ViewModel(['form' => $this->updateForm]);
}

Should be:

if ( ! $this->registerForm->isValid($prg) ) {
    return new ViewModel(['form' => $this->registerForm]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.