0
votes

Controller code


    if(!empty($this->data)){
        if($this->{$this->modelClass}->signupValidate()){
           $this->{$this->modelClass}->save($this->data);
        }
    }

Model Code


    function signupValidate(){      
            $validate1  =   array(
                'first_name' => array(
                    'rule1' => array(
                        'rule' => 'notEmpty',
                        'message' => __('Please enter first name',true)
                    )
                )
            );
    $this->validate     =   $validate1;     
    return $this->validates();
    }

Validation not working properly

2
Please, show the error log and tell what you wanna do.heloisasim
Solved by $this->{$this->modelClass}->set($this->data); this lineSmith

2 Answers

0
votes

you should set in your controller


    $this->{$this->modelClass}->set($this->data);

Like this



    if(!empty($this->data)){
        $this->{$this->modelClass}->set($this->data);
        if($this->{$this->modelClass}->signupValidate()){
           $this->{$this->modelClass}->save($this->data);
        }
    }

0
votes

Your model will automatically call validations before saving the data, if not then you can use the following code in your controller

In Your Controller

$this->loadModel('YourModel');
if($this->YourModel->validates())
{
    $this->YourModel->save($this->data); 
}

Add the code below in your model class

var $validate = array(
        'first_name' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        ),
        'last_name' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        ),
        'phone' =>  array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'Phone number should be valid.'
            ),
            'phone' => array(
                'rule' => array('phone', null, 'us'),
                'message' => 'Phone number should be valid e.g. 555-555-5555'
            )
        ),
        'email' => array(
            'email' => array(
                'rule' => 'email',
                'message' => 'Please enter a valid email address'
            ),
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank'
            ),
            'validEmail' => array(
                'rule' => array('validEmail'),
                'message' => 'Email address does not exist.'
            )
        ),
        'captcha_code' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank'
            )
        ),
        'address' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        ),
        'city' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        ),
        'street' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        )

    );