Reputation: 193
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
Reputation: 95
Everything in model looks OK. May be try
echo $model->getErrors();
in your controller.May be can help you.
Upvotes: 0
Reputation: 4160
Change Your Rule Property As:
public function rules()
{
return [
[['product_price'], 'checkMaxPrice' ,'skipOnEmpty' => false]
];
}
To know Skip On Empty
Upvotes: 2