0

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
  • 1
    Please, show the error log and tell what you wanna do. Commented Mar 16, 2016 at 12:17
  • Solved by $this->{$this->modelClass}->set($this->data); this line Commented Mar 17, 2016 at 6:01

2 Answers 2

0

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);
        }
    }

Sign up to request clarification or add additional context in comments.

Comments

0

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.'
            )
        )

    );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.