user2636556
user2636556

Reputation: 1915

Cant get Yii Model custom rules to work

In a form i working on, i have this rules set which i want to validate. rate should be a a floating number. and price should be numbers with , and .

here is what i have so far

public function rules()
{
    array('rate','type', 'type'=>'float', 'on' => 'loan-calculator'),

    // EPriceValidator is a custom validation class
    array('price', 'site.common.components.validate.EPriceValidator'),
}

In site.common.components.validate.EPriceValidator i have this

class EPriceValidator extends CRegularExpressionValidator
{
        public $pattern = '/[^0-9,.]/';
}

when i change

array('price', 'site.common.components.validate.EPriceValidator'),

to

array('price', 'match', 'not' => true, 'pattern' => '/[^0-9,.]/'),

it works perfectly when validating on the fly. but i would rather put it into a class, this way i can reuse it thru out my site.

array('rate','type', 'type'=>'float', 'on' => 'loan-calculator'),

the code above on the other hand doesn't work at all. Any idea how i can fix these two problems? Or what am i'm doing wrong? Thanks

Upvotes: 1

Views: 183

Answers (1)

srakl
srakl

Reputation: 2619

try something like this

class EPriceValidator extends CValidator
{

    //Regular Expressions for numbers
    private $pattern = '/[^0-9,.]/';

     //Default error messages 
     private $err_msg = '{attribute} is an invalid.';

    /**
     * Validates the attribute of the object.
     * If there is any error, the error message is added to the object.
     * @param CModel $object the object being validated
     * @param string $attribute the attribute being validated
     */
    protected function validateAttribute($object,$attribute)
    {
        $pattern = $this->pattern;

        // extract the attribute value from it's model object
        $value = $object->$attribute;

        if(!preg_match($pattern, $value))
        {
            $this->addError($object, $attribute, $this->err_msg);
        }
    }

    /**
     * Implementing Client Validation
     *
     * Returns the JavaScript needed for performing client-side validation.
     * @param CModel $object the data object being validated
     * @param string $attribute the name of the attribute to be validated.
     * @return string the client-side validation script.
     * @see CActiveForm::enableClientValidation
     */
    public function clientValidateAttribute($object,$attribute)
    {

        // check the strength parameter used in the validation rule of our model
        $pattern = $this->pattern; 

        //replace {attribute} with correct label
        $params['{attribute}']=$object->getAttributeLabel($attribute);
        $error_message = strtr($this->err_msg,$params);

        return "
        if(value.match(".$pattern.")) {
            messages.push(".CJSON::encode($error_message).");
        }
        ";
    }
}

the validateAttribute() function overrides the CValidator class function for server side validation. And the clientValidateAttribute() function overrides the CValidator class function for client side validation.

for more information read this

Upvotes: 1

Related Questions