Validation Not Working

I’m new to Yii framework. Now I’m using two textfields for


min size

and


max size

. Now this textfields are supposed to take only integers. But they are taking alphabets also. I used the following rule in model file.




 public function rules() {

return array(

    array('min_size, max_size', 'numerical', 'integerOnly'=>true));

    }




But this seems to be not working, no error is displayed . How can i validate successfully, by displaying error when text is entered. Should I make some changes in main.php

Can you show your action code and the relevant part of the view code?

I dont see this line or function inside the controller. Should I add this $this->performAjaxValidation($model);

View code-




<?php echo $form->textField($model,'min_size', array('placeholder' => 'Min Sqft', array('integerOnly'=>'true')); ?><?php echo $form->textField($model,'max_size', array('placeholder' => 'Min Sqft', array('integerOnly'=>'true')); ?>



Action code - is it controller code ?

Yes, the action is the function in the controller that processes the request.

Where are your errors being output in the view?

You may want to add the $this->performAjaxValidation($model) functionality to the action, assuming that you’ve configured the form to use ajax validation.

Controller code -




protected function performAjaxValidation($model)

        {

                if(isset($_POST['ajax']) && $_POST['ajax']==='yw0')

                {

                        echo CActiveForm::validate($model);

                        Yii::app()->end();

                }

        }




    /**

     * This is the default 'index' action that is invoked

     * when an action is not explicitly requested by users.

     */

    public function actionIndex() {

        

        $search_model = new SearchForm();        

        $this->performAjaxValidation($search_model);

        

        if (isset($_GET['transaction']) && $_GET['transaction'] == "rental") {

            $transaction = "rental";

        } else {

            $transaction = "resale";

        }

            

        if (isset($_GET['SearchForm'])) {

            

	

            $search_model->attributes = $_GET['SearchForm'];

            $xml = new SimpleXMLElement('<searchreq/>');

    

            if (isset($_GET['SearchForm']['min_size']) && isset($_GET['SearchForm']['max_size'])) {                

                $area = $xml->addChild('area');

                $area->addChild('min', $_GET['SearchForm']['min_size']?$_GET['SearchForm']['min_size']:"0");

                $area->addChild('max', $_GET['SearchForm']['max_size']?$_GET['SearchForm']['max_size']:"500000000");

      

            } else {

                $area = $xml->addChild('area');

                $area->addChild('min', 0);

                $area->addChild('max', 5000000);

            }

            

        

        

        

        $this->render('index', array(

            'dataProvider' => $dataProvider,

            'model' => $search_model,

            'transaction' => $transaction

        ));

    }

    



And the view error output code?

Keith, it is not validating and it is accepting all data in the view and not displaying any errors.

Okay, but you should have code which is attempting to output errors in the view. Even if ajax validation isn’t working, the errors should be output on page reload. Try using this somewhere in your view:




<? echo CHtml::errorSummary($model); ?>



Also, as far as I can see, you’re not performing any validation in your action on a regular page request, which is very dangerous. Make sure that you validate the model before using any of the submitted form data.

try this if you want to add float and numeric valiation


array('percent', 'numerical', 'allowEmpty' => true,

                                'integerOnly' => false, 'min' => 0, 'max' => 100),