kritika555
kritika555

Reputation: 193

yii2 custom validation in rule not working

How to implement custom validation in yii2?

My code in model rules is

public function rules()
{
    return [
        [['product_price'], 'checkMaxPrice']
    ];
}

public function checkMaxPrice($attribute,$params)
{
    if($this->product_price > 1000) {
        $this->addError($attribute,'Price must be less than 1000');
    }
}

Anything else I have to do in view?

Upvotes: 0

Views: 1157

Answers (2)

Mad Adh
Mad Adh

Reputation: 95

Everything in model looks OK. May be try

echo $model->getErrors();

in your controller.May be can help you.

Upvotes: 0

Double H
Double H

Reputation: 4160

Change Your Rule Property As:

public function rules()
{
    return [
        [['product_price'], 'checkMaxPrice' ,'skipOnEmpty' => false]
    ];
}

To know Skip On Empty

Upvotes: 2

Related Questions