Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii validation rules for min value or empty

Tags:

yii

I want to add a rule where user can post empty value, or integer value shoud be in range min-max. Is this possible without custom rules?

So, if min is 5 and max is 10, user could post values: empty, 5,6,7,8,9 or 10.

like image 479
M-K Avatar asked Dec 06 '25 06:12

M-K


2 Answers

You can try to use numerical validator by setting its min to 5, max to 10, allowEmpty and integerOnly to true.

like image 87
ezze Avatar answered Dec 09 '25 14:12

ezze


Try this -

public function rules() {
        return array(
            array('test', 'safe'),
            array('test', 'length', 'min' => 5, 'max'=>10, 
            'tooShort'=>Yii::t("translation", "{attribute} to short."),
            'tooLong'=>Yii::t("translation", "{attribute} to long.")),

            array('test', 'match', 'pattern'=>'/^[0-9]+$/'), 
        );
    }
like image 39
Rohit Suthar Avatar answered Dec 09 '25 14:12

Rohit Suthar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!