how we can add rule in Yii model for input must be greater than 0

Shadman picture Shadman · Mar 29, 2012 · Viewed 20.6k times · Source

do anyone know how can I apply rule in Yii model for input must be greater than 0 value, without any custom approach ..

like :

public function rules()
{
    return array( 
        ....
        ....

            array('SalePrice', 'required', "on"=>"sale"),

        ....
        ....
    );
}

many thanks ..

Answer

adamors picture adamors · Mar 29, 2012

Simpler way array('SalePrice', 'numerical', 'min'=>1)

with a custom validator method

array('SalePrice', 'greaterThanZero')

 public function greaterThanZero($attribute,$params)
   {

      if ($this->$attribute<=0)
         $this->addError($attribute, 'Saleprice has to be greater than 0');

 }