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

Validation issue

Hi,

I am created one project in which On 1.3 version of Phalcon code is working fine.My error Messages are show on screen. But when I try for 2.0 version for another Machine it is not showing error messages. I call It from View using

$form->messages("user_dob");

If I print this value in controller It display the value.

I used beforevalidation function while validation function that is :


if (isset($data['user_dob']) && $data['user_dob']!='') {

            $elements['user_dob']->addValidators(array( new \Phalcon\Validation\Validator\Regex(
                    array(
                            'field'  => 'user_dob',
                            'pattern' => '/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/',
                            'message' => 'Invalid date format'
                    ))
            ));

            $dob = array_reverse(explode('/', $data['user_dob']));
            $dob_nw = implode('-', $dob);

            $now_dt = date_create(date('Y-m-d'));
            $dob_dt = date_create($dob_nw);

            $interval = date_diff($now_dt, $dob_dt);
            $age = (int) $interval->format('%y');
                        //echo "test-->$age";exit;

            if ( $age < USER_AGE_LIMIT) {

                $elements['user_dob']->addValidators(array( new \Phalcon\Validation\Validator\Regex(
                        array(
                                'field'  => 'user_dob',
                                'pattern' => '/[0-9]/',
                                'message' => 'Age shuld be greater than 18'
                        ))
                ));

            }
        }

This code is used for chevk validation.

Please Help me to Solve this problem.

edited May '15

Hi Friends,

I initailize the form varible in initialize function.

I use forward method method which return reinitialize the form.

My code is Previus code

public function initialize()
    {
        global $di;

        $this->form = new RegisterForm();    
        parent::initialize();
    }
My current changes are 

public function initialize()
    {
        global $di;
        if(!isset($this->form))   
            $this->form = new RegisterForm();

        parent::initialize();
    }

Pls tell me is the correct method. I am not sure about it that it is correct or not . Is there any another method to Solve this problem