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

6 Answers 6

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

 }
3
  • 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, 2012 at 11:53
  • No, if the validator is 'numerical' min means lower limit. See here: yiiframework.com/doc/api/1.1/CNumberValidator#min-detail
    – adamors
    Mar 29, 2012 at 18:31
  • There is a CCompareValidator that can do this out of the box.
    – Arth
    May 7, 2015 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

3
  • 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 7, 2014 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, 2015 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, 2015 at 7: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.

0

Did nobody check the docs?

There is a built-in validator CCompareValidator:

['SalePrice', 'compare', 'operator'=>'>', 'compareValue'=>0]
0

you can use this one too:

array('SalePrice', 'in','range'=>range(0,90))
-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 ..

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.