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

20,555

Solution 1

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

 }

Solution 2

I see it is a price so you could use 0.01 (a penny) as a minimum value like so:

array('SalesPrice', 'numerical', 'min'=>0.01),

Note that this solution does not validate that the number entered is a price, just that it is > 0.01

Share:
20,555
Shadman
Author by

Shadman

An experienced Software Engineer/Architect, Techie, Entrepreneur, Traveller. Having worked within the industry for over 10.5 years. crazy about softwares &amp; startups

Updated on July 09, 2022

Comments

  • Shadman
    Shadman almost 2 years

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