Formmodel >> Check Other Fields Are Valid Inside A Custom Validation

Hi all,

Let’s assume that I have a form with a, b & c fields. I’m going to validate c using custom validation. Inside there, I need to check whether both a & b are already valid. That means, i need to validate c only if a & b are valid.

Following is a sample of my code

public function rules() {

        return array(

                array('a', 'required'),

                array('b', 'numerical', 'min' => 18, 'max' => 99),			

                array('c', 'mycustomvalidation')




public function mycustomvalidation($attribute, $params) {

    if($this->validate(array('a', 'b'))) { // DO THE VALIDATION IF a & b ARE VALID ONLY

        $error = '';

        if($this->attributes[$attribute] > 1000)

            $error = $this->getAttributeLabel($attribute) . " must be lower than 1000.";

        if (!empty($error))

            $this->addError($attribute, $error);



Please help me to find a solution for this. Thank you!

You can use CModel.getErrors() to check if a specific attribute has any validation errors.

Thank you very much for the tip. I used hasErrors function instead of getErros.

All the best!