We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

What am I doing wrong trying to validate this form?

Hi. I'm new to Phalcon and I'm trying to get this form to validate. I am following the example set forward by the Vokuro example, but I'm having some troubles.

My form is declared as a class:

    <?php

    use Phalcon\Forms\Form;
    use Phalcon\Forms\Element\Text;
    use Phalcon\Forms\Element\Hidden;
    use Phalcon\Forms\Element\Password;
    use Phalcon\Forms\Element\Submit;
    use Phalcon\Forms\Element\Check;
    use Phalcon\Forms\Element\Email as EmailField;
    use Phalcon\Validation\Validator\PresenceOf;
    use Phalcon\Validation\Validator\Email;
    use Phalcon\Validation\Validator\Identical;
    use Phalcon\Validation\Validator\StringLength;
    use Phalcon\Validation\Validator\Confirmation;

    class RegistrationForm extends Form {

        public function initialize($entity = null, $options = null) {
            $this->usernameField();
            $this->emailField();
            $this->passwordField();
            $this->confirmPasswordField();
            $this->termsField();
            // $this->csrfField();
            $this->submitButton();
        }

        private function usernameField() {
            $field = new Text('username');

            $field->setLabel('Username');

            $field->addValidators([
                new PresenceOf([
                    'message' => 'Username is required.'
                ])
            ]);

            $this->add($field);
        }

        private function emailField() {
            $field = new EmailField('email');

            $field->setLabel('Email');

            $field->addValidators([
                new PresenceOf([
                    'message' => 'Email is required.'
                ]),
                new Email([
                    'message' => 'Email format is invalid.'
                ])
            ]);

            $this->add($field);
        }

        private function passwordField() {
            $field = new Password('password');

            $field->setLabel('Password');

            $field->addValidators([
                new PresenceOf([
                    'message' => 'Password is required.'
                ]),
                new StringLength([
                    'min' => 5,
                    'messageMinimum' => 'Password must be 5 characters or longer.'
                ])
            ]);

            $this->add($field);
        }

        private function confirmPasswordField() {
            $field = new Password('confirm_password');

            $field->setLabel('Confirm Password');

            $field->addValidators([
                new PresenceOf([
                    'message' => 'Confirm password is required.'
                ]),
                new Confirmation([
                    'message' => 'Both password must match.',
                    'with' => 'password'
                ])
            ]);

            $this->add($field);
        }

        private function termsField() {
            $field = new Check('terms', [
                'value' => 'yes'
            ]);

            $field->setLabel('Accept terms and condition?');

            $field->addValidator(new Identical([
                'value' => 'yes',
                'message' => 'You must accept terms and condition to register.'
            ]));

            $this->add($field);
        }

        private function csrfField() {
            $field = new Hidden('crsf');

            $field->addValidator(new Identical([
                'value' => $this->security->getSessionToken(),
                'message' => 'CSRF validation failed.'
            ]));

            $this->add($field);
        }

        private function submitButton() {
            $this->add(new Submit('register', [
                'value' => 'Register'
            ]));
        }

        /**
         * Prints messages for a specific element
         */
        public function messages($name)
        {
            if ($this->hasMessagesFor($name)) {
                foreach ($this->getMessagesFor($name) as $message) {
                    $this->flash->error($message);
                }
            }
        }
    }

I am outputting these fields in the view one by one using $form->render(). However, I am receiving the following exception when calling $form->isValid() - PhalconException: There are no data to validate.

What I am doing wrong? In the example, it's just checking if $form->isValid() and then passing it directly to the model.



6.6k
Accepted
answer

You have to pass data to the method $form->isValid():

<?php

if ($this->request->isPost()) {
    if ($form->isValid($this->request->getPost())) {
        // data processing...
    }
}

Call $form->isValid() without params when binding data to a model directly:

<?php

if ($this->request->isPost()) {
    $model = new YourModel();
    $form->bind($this->request->getPost(), $model);
    if ($form->isValid()) {
        // save the model or do something else here...
    }
}


12.6k

@skollro - Okay, thank you. I didn't see that step in the example I am following so I was a bit confused. I really appreciate you clarifying that for me.

Thanks!