enableClientValidation not working

I’m having difficulty getting automatic client validation to work. I’m not the only person working on this project, so there’s a chance something has been damaged, removed, etc. without my knowledge. However, the framework itself should be intact.

In summary, this is what I have in the Controller for the action in question:




    public function actionPresentation() {

        $model = new PresentationForm;

        $this->handleAjaxValidation($model);

        if (isset($_POST['PresentationForm']))

        {

            $model->attributes = $_POST['PresentationForm'];

            if ($model->validate()) {

                $result = Mail::sendFormModel($model);

                Yii::app()->user->setFlash('presentation',$result);

                $this->refresh();

            }

        }

        $this->render('presentation', array('model'=>$model));

    }


    // Incomplete

    public function handleAjaxValidation($model) {

        if (isset($_POST['ajax'])) {

            echo $_POST['ajax'];

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

        }

    }



Here is what I have in the view.




    <?php $form=$this->beginWidget('ActiveForm', array(

        'id'=>'presentation-form-presentation-form',

        'enableAjaxValidation'=>true,

        'enableClientValidation'=>true,

        'focus'=>array($model,'firstName'),

    )); ?>



I load the page and focus works. In the source, the jquery script file is sourced, but not the yiiactiveform script file. No client validation seems to happen. Also, I have enableAjaxValidation on too, which I have not fully implemented in the Controller, but it does not seem to even try anyway. In the framework, I only find that yiiactiveform script mentioned once. It’s in the run() method of framework/web/widgets/CActiveForm.php. Here is the code:




    public function run()

    {

        if(is_array($this->focus))

            $this->focus="#".CHtml::activeId($this->focus[0],$this->focus[1]);


        echo CHtml::endForm();

        $cs=Yii::app()->clientScript;

        if(!$this->enableAjaxValidation && !$this->enableClientValidation || empty($this->attributes))

        {

            if($this->focus!==null)

            {

                $cs->registerCoreScript('jquery');

                $cs->registerScript('CActiveForm#focus',"

                    if(!window.location.hash)

                        $('".$this->focus."').focus();

                ");

            }

            return;

        }


        $options=$this->clientOptions;

        if(isset($this->clientOptions['validationUrl']) && is_array($this->clientOptions['validationUrl']))

            $options['validationUrl']=CHtml::normalizeUrl($this->clientOptions['validationUrl']);


        $options['attributes']=array_values($this->attributes);


        if($this->summaryID!==null)

            $options['summaryID']=$this->summaryID;


        if($this->focus!==null)

            $options['focus']=$this->focus;


        $options=CJavaScript::encode($options);

        $cs->registerCoreScript('yiiactiveform');

        $id=$this->id;

        $cs->registerScript(__CLASS__.'#'.$id,"\$('#$id').yiiactiveform($options);");

    }




I did some debugging and found that empty($this->attributes) ends up being true. This makes me wonder if I’m doing something wrong, but thus far have not found an answer, despite asking around.

So, any help would be greatly appreciated!

Scan Activeform.php for changes that may affect ajax validation.

/Tommy

It only overrides init() to register form.css. It also calls parent::init().

Check if scriptMap (or packages) are defined/redefined somewhere.

/Tommy

The strings ‘scriptMap’ and ‘packages’ do not appear anywhere in the application directory (just in the framework, which has not been altered). Does that rule this out, or is there something else I can look at? I’m not familiar with scriptMap or how the packages work.

If yiiactiveform js code is not included in the page then client validation and ajax validation will not work as that is processed from there… you need to find the reason why that file is not registerd…

If you can try to use CActiveForm to see if the script will be included in the generated page…

If not… then check with other developers to see who and why did disable the yiiactiveofrm js code…

Yes, that’s why I’m here.

The same problem exists when using CActiveForm.

No one disabled it intentionally, but it is possible something was changed that inadvertently caused this problem. If you have any ideas on what this might be, I’d be happy to investigate it.

Unfortunately, the problem is still there and I have yet to find a solution.

I recommend some structure to your approach. We don’t really have enough information, so you’ll need to go in and narrow things down.

First, you should try to rule out problems in the core framework. Redownload it, replace it, and see if that works. (Or better yet, make sure no one changed anything in the core!)

Second, you need to narrow it down to that specific controller/model/view or the base controller/model/view. Create a new test table (ie, with two fields such as "id" and "name"), and then create the model + crud for it using gii and see if that works. If it works, then the base controller/model/view is fine.

I have appeared in this issue,you have to ensure the following:

  1. echo $form->error($[size=2]model,’[/size][color=#008800][size=2]firstName[/size][/color][size=2]’[/size]) : you have to use this sentence in your view (directly),it will bind the CActiveFom’s attributes property!

2.The above sentence must be placed in front of "<?php $this->endWidget(); ?>"!