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

Phalcon Form Validation Error Message

Hi guys, I have a question about form validation, why is it that validation messages still appear even if I already enter the right value in a form, i.e. an email textbox , i entered [email protected] and hit the send button but still error message is there instead it should disappear. I want to achieve this by using phalcon form validation and not by javascript or jquery. thnks :)

here is my code

<?php

use \Phalcon\Forms\Form,
    \Phalcon\Forms\Element\Text,
    \Phalcon\Forms\Element\Password,
    \Phalcon\Forms\Element\Email,
    \Phalcon\Forms\Element\Radio,
    \Phalcon\Forms\Element\Select,
    \Phalcon\Forms\Element\Submit,
    \Phalcon\Validation,
    \Phalcon\Validation\ValidatorInterface,
    \Phalcon\Validation\Validator\PresenceOf,
    \Phalcon\Validation\Validator\StringLength,
    \Phalcon\Validation\Validator\Confirmation,
    \Phalcon\Validation\Message,
    \Phalcon\Escape,
    \Phalcon\Mvc\Model,
    \Phalcon\Db\RawValue,
    \Phalcon\Db\Column,
    \Phalcon\Http\Request as FormRequest;

class RegisterForm extends Form{

    private static $reg_frm = [];
    private static $request;

    public function initialize(){

        /* Fullname */
        self::$reg_frm['fullname'] = new Text('fullname',[
                                        'maxlength'     =>  255,
                                        'placeholder'   =>  '',
                                        'class'         =>  'form-control txt-name',
                                        'autocomplete'  =>  'off',
                                        'autofocus'     =>  'autofocus'
                                    ]
                                );
        /* Email */
        self::$reg_frm['email'] = new Email('email', [
                                        'maxlength'     =>  25,
                                        'placeholder'   =>  '',
                                        'class'         =>  'form-control txt-email',
                                        'autocomplete'  =>  'off'
                                    ]);
        /* Password */
        self::$reg_frm['password'] = new Password('password', [
                                        'maxlength'     =>  25,
                                        'placeholder'   =>  '',
                                        'class'         =>  'form-control txt-password',
                                        'autocomplete'  =>  'off'
                                    ]);
        /* Re Password */
        self::$reg_frm['repassword'] = new Password('repassword', [
                                        'maxlength'     =>  25,
                                        'placeholder'   =>  '',
                                        'class'         =>  'form-control txt-password',
                                        'autocomplete'  =>  'off'
                                    ]);
        /* Male */
        self::$reg_frm['gender'] = new Radio('gender1', [
                                        'value' => 'male',
                                        'name'  => 'gender',
                                        'class' => 'male'
                                    ]);
        /* Female */
        self::$reg_frm['gender'] = new Radio('gender2', [
                                        'value' => 'female',
                                        'name'  => 'gender',
                                        'class' => 'female'
                                    ]);
        /* Country */
        self::$reg_frm['country'] = new Select('country', 
                                        TblCountry::find(array(
                                            'columns' => 'country_name'
                                        )), 
                                        array(
                                            'useEmpty'  => true,
                                            'emptyText' => '',
                                            'using'     => array('country_name', 'country_name'))
                                    );
        self::$reg_frm['submit'] = new Submit('submit', [
                                        'value' => 'Submit',
                                        'class' => 'btn btn-primary',
                                        'id'    => 'btn'
                                    ]);

        /* Form labels */
        self::$reg_frm['fullname']->setLabel('Fullname :');
        self::$reg_frm['email']->setLabel('Email :');
        self::$reg_frm['password']->setLabel('Password :');
        self::$reg_frm['repassword']->setLabel('Re-Password :');
        self::$reg_frm['gender']->setLabel('Male :');
        self::$reg_frm['gender']->setLabel('Female :');
        self::$reg_frm['country']->setLabel('Country :');

        /* Add all form element */
        foreach (self::$reg_frm as $formelement) {

            $this->add($formelement);
        }

        self::$request = new FormRequest;

        $validation = new Validation();

        $validation
            ->add(self::$reg_frm['fullname'], new PresenceOf(array(
                'message' => 'Fullname is required'
            )))
            ->add(self::$reg_frm['email'], new PresenceOf(array(
                'message' => 'Email is required'
            )))
            ->add(self::$reg_frm['password'], new PresenceOf(array(
                'message' => 'Password is required'
            )))
            ->add(self::$reg_frm['password'], new Confirmation(array(
               'message' => 'Password doesn\'t match confirmation',
               'with' => 'confirmPassword'
            )))
            ->add(self::$reg_frm['repassword'], new PresenceOf(array(
                'message' => 'Confirm is required'
            )))
            ->add(self::$reg_frm['country'], new PresenceOf(array(
                'message' => 'Country is required'
            )));

        $messages = $validation->validate($_POST);

        if (count($messages)) {
            foreach ($messages as $message) {
                echo'<p class="p-error">'.$message.'</p>';
            }
        }
    }
}
edited Nov '14

I don't know if this will be a solution to your problem but normally the way you define and use forms is a bit different.

First you have to create the form object e.g.

<?php
class BlogForm extends Form {
    public function initialize($post)
    {
        // field for author of blog post
        $field = new \Phalcon\Forms\Element\Text('author');

        $field->addValidator(new \Phalcon\Validation\Validator\PresenceOf(array(
            'message' => 'The name is required'
        )));

        $this->add($field);

        // more fields ...
    }
}

And then in the controller you use this form e.g.

<?php
class BlogController extends \Phalcon\Mvc\Controller {

    // ...
    public function editAction($id) {
        $post = Blog::findFirstById($id);

        // get instance of form
        $form = new BlogForm($post);

        // check if form was posted
        if ($this->request->isPost()) {

            // validation of post data against form definition
            if ($form->isValid($this->request->getPost()) != false) {

                $form->bind($this->request->getPost(), $post);

                if ($post->save()) {
                    return $this->response->redirect('blog/show/' . $id);
                }
            } else {

                // if validation fails write messages to flash service
                foreach ($form->getMessages() as $message) {
                    $this->flash->error((string) $message);
                }
            }
        }

        $this->view->post = $post;
        $this->view->form = $form;
    }

    // ...
}