4

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

Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
Shadman
  • 755
  • 2
  • 11
  • 19

6 Answers6

8

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

 }
adamors
  • 2,672
  • 5
  • 32
  • 45
  • i think min attribute is use for minimum length in textbox .. i need to validate for value number must be greater than 0 no need for length – Shadman Mar 29 '12 at 11:53
  • No, if the validator is 'numerical' min means lower limit. See here: http://www.yiiframework.com/doc/api/1.1/CNumberValidator#min-detail – adamors Mar 29 '12 at 18:31
  • There is a CCompareValidator that can do this out of the box. – Arth May 07 '15 at 14:42
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

sdjuan
  • 709
  • 6
  • 15
  • Looks cool but a value of `1.2345689` will pass this rule .. and is not a valid price. Your validation will only limit cases like 0.009 which are not a real scenario. – d.raev Dec 07 '14 at 16:44
  • The context of the OP was for price, then you suggest a cool looking but WRONG validation specifically for **price** .. and then excuse your self that it may not work for price .. but work .. in some general case .. that is not what OP asked and not what you are suggesting !? Your post should be edited or even deleted, as it gives cool looking but totally wrong solution. And will not be noticed immediately after a copy-paste of the code .. so it makes it evil. – d.raev Jan 19 '15 at 11:25
  • @d.raev actually the OP did not specify that the rule need validate a price, the op instead asked for "how can I apply rule in Yii model for input must be greater than 0 value" and did that twice, once in the title and then again in the body. It was i who picked the price part out. And for what it is worth 1.2345689 is > zero so should pass the rule the OP asked for "input must be greater than 0 value" . Thanks for your suggestion I will add a note that this does not validate that the number is a price even though that is not asked for. – sdjuan Jan 27 '15 at 07:22
0

I know I am too late for this . But just for future reference you can use this class also

<?php
class greaterThanZero extends CValidator 
{
/**
 * Validates the attribute of the object.
 * If there is any error, the error message is added to the object.
 * @param CModel $object the object being validated
 * @param string $attribute the attribute being validated
*/
 protected function validateAttribute($object,$attribute)
  {
    $value=$object->$attribute;
     if($value <= 0)
    {
    $this->addError($object,$attribute,'your password is too weak!');
}
 }


  /**
  * Returns the JavaScript needed for performing client-side validation.
  * @param CModel $object the data object being validated
  * @param string $attribute the name of the attribute to be validated.
   * @return string the client-side validation script.
  * @see CActiveForm::enableClientValidation
*/
 public function clientValidateAttribute($object,$attribute)
 {

    $condition="value<=0";
     return "
   if(".$condition.") {  messages.push(".CJSON::encode($object->getAttributeLabel($attribute).' should be greater than 0').");
 }";
}

 }

?>

Just make sure this class is imported before use.

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
0

Did nobody check the docs?

There is a built-in validator CCompareValidator:

['SalePrice', 'compare', 'operator'=>'>', 'compareValue'=>0]
Arth
  • 12,789
  • 5
  • 37
  • 69
0

you can use this one too:

array('SalePrice', 'in','range'=>range(0,90))
Ebrahim Poursadeqi
  • 1,776
  • 2
  • 17
  • 27
-2

I handled this by regular expression, may be it will help too ..

array('SalePrice', 'match', 'not' => false, 'pattern' => '/[^a-zA-Z0]/', 'message' => 'Please enter a Leader Name', "on"=>"sale"),

many thanks @sdjuan & @Ors for your help and time ..

Shadman
  • 755
  • 2
  • 11
  • 19