Yii - How to validate a textfield for integers only

user3004356 picture user3004356 · Nov 20, 2013 · Viewed 9.5k times · Source

The below model extends CFormModel and I need to mention rules to it such that the attributes allow only integers.

class SearchForm extends CFormModel {

    public $min_size;
    public $max_size;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
array('min_size, max_size', 'numerical', 'integerOnly'=>true),

        );
    }

The textfield is created using -

<?php echo $form->textField($model,'min_size', array('placeholder' => 'Min Sqft', 'style'=>'width:100px')); ?>  

But, the above validation is not working. How can I validate the text field to allow integers only. Is there anyway I can do validation

Answer

M Gaidhane picture M Gaidhane · Nov 20, 2013

try this

public function rules(){
return array(
    array('min_size, max_size', 'numerical', 'integerOnly'=>true));}