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

Form event validation

Ho can i use event validation on my form object, my code is not working even if i already use

\Phalcon\Validation,

Here is my code, the afterValidation is not echoing any message on my screen when i click submit button


<?php
use \Phalcon\Forms\Form as 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;

class RegisterForm extends Form{

    private static $reg_frm = [];

    public function initialize(){

        /* Fullname */
        self::$reg_frm['fullname'] = new Text('fullname', [
                                        'maxlength'     =>  255,
                                        'placeholder'   =>  '',
                                        'class'         =>  'form-control txt-name',
                                        'autocomplete'  =>  'off',
                                        'autofocus'     =>  'autofocus'
                                    ]);
        self::$reg_frm['fullname']->setLabel('Fullname :');

        /* Email */
        self::$reg_frm['email'] = new Email('email', [
                                        'maxlength'     =>  25,
                                        'placeholder'   =>  '',
                                        'class'         =>  'form-control txt-email',
                                        'autocomplete'  =>  'off',
                                        'message'       =>  'Not valid email'
                                    ]);
        self::$reg_frm['email']->setLabel('Email :');

        /* Password */
        self::$reg_frm['password'] = new Password('password', [
                                        'maxlength'     =>  25,
                                        'placeholder'   =>  '',
                                        'class'         =>  'form-control txt-password',
                                        'autocomplete'  =>  'off'
                                    ]);
        self::$reg_frm['password']->addValidators([
                                        new PresenceOf([
                                            'message'   =>  'Password is required'
                                        ]),
                                        new StringLength([
                                            'min'               =>  8,
                                            'messageMinimum'    =>  'Password is too short. Minimum 8 characters'
                                        ]),
                                        new Confirmation([
                                            'message'   =>  'Password doesn\'t match confirmation',
                                            'with'      =>  'repassword'
                                        ])
                                    ]);
        self::$reg_frm['password']->setLabel('Password :');

        /* Re Password */
        self::$reg_frm['repassword'] = new Password('repassword', [
                                        'maxlength'     =>  25,
                                        'placeholder'   =>  '',
                                        'class'         =>  'form-control txt-password',
                                        'autocomplete'  =>  'off'
                                    ]);
        self::$reg_frm['repassword']->setLabel('Re-Password :');

        /* Male */
        self::$reg_frm['gender1'] = new Radio('gender1', [
                                        'value' => 'male',
                                        'name'  => 'gender',
                                        'class' => 'male'
                                    ]);
        self::$reg_frm['gender1']->setLabel('Male :');

        /* Female */
        self::$reg_frm['gender2'] = new Radio('gender2', [
                                        'value' => 'female',
                                        'name'  => 'gender',
                                        'class' => 'female'
                                    ]);
        self::$reg_frm['gender2']->setLabel('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['country']->setLabel('Country :');

        self::$reg_frm['submit'] = new Submit('submit', [
                                        'value' => 'Send',
                                        'class' => 'btn btn-primary',
                                        'id'    => 'btn'
                                    ]);

        foreach (self::$reg_frm as $formelement) {

            $this->add($formelement);
        }
    }

    public function afterValidation(){

        $request = new Request();

        if($request->isPost() == true){

            echo"message";
        }
    }
}

Try after echo "message"; add exit;