Search code examples
validationyiimodelyii-validation

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


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 ..


Solution

  • 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');
    
     }