Formbuilder. Ajaxvalidate Doesn't Work With Showerrorsummar Switched To Truey

Hi,

I’m using Yii formbuilder which I think is a great tool. The problem is that ajax-Validation doesn’t work anymore once I set showErrorSummary to true in the CForm config file. Any ideas what I’m doing wrong?

My controller:




public function actionCreate() {

    $model=new House;

    if(isset($_POST['ajax']) && $_POST['ajax']==='house-form') {

	echo CActiveForm::validate($model);

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

    }    

    $form=new CForm('application.modules.house.views.form',$model);

    ...

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

	'form'=>$form

));

}

My config file form:




return array(

    'title'=>'New House',

    'showErrorSummary'=>true,

    'activeForm'=>array(

	'class'=>'CActiveForm',

	'enableAjaxValidation'=>true,

	'id'=>'house-form',

        'clientOptions'=>array(

	    'validateOnSubmit'=>true,

	),

    ),

    'elements'=>array(

	'email'=>array(

	    'type'=>'text',

	    'size'=>35,

	    'maxlength'=>80,

	),

	'name'=>array(

	    'type'=>'text',

	    'size'=>35,

	    'maxlength'=>80,

	),

        ...

    ),

    'buttons'=>array(

        ...

    ),

);



My create view




echo $form->render(); 



try this one…its working fine with me for ajax validation


'activeForm' => array(

    	'class' => 'CActiveForm',

    	'enableAjaxValidation' => false,

    	'enableClientValidation' => true,

    	'clientOptions' => array(

        	'validateOnSubmit' => true,

        	'validateOnChange' => false,

        	'validateOnType' => false,

    	),

    	'id' => 'new-form',

	), 

Awesome! That works for me. Thanks a lot!

Welcome !!!

Thanks for your welcome.

Unfortunately cheered too soon. While testing your proposal showErrorSummary was still set to false. It stopped working again once I set it to true. Any help apprechiated.

Why you are doing showErrorSummary => true

still showing errors without showErrorSummary means if u don’t declare showErrorSummary in

ur formbuilder config file so it will also display error with each field correspondingly using ajax

Of course, you are right: errors are shown next to the corresponding field. But imagine you got a very large form where an error occurs at its very bottom. In that case a user might fail to notice there was an error (just a question of usability).

Both, ajax validation and errorSummary, go smoothly together when handling a form in the conventional way (without formbuilder). So, I wonder if there is a way to make all these compoments work together.

After a lot of research I still can’t figure out what is going wrong. This is what I found so far:

The moment I set showErrorSummaray to true neither jquery.js nor yiiactiveform.js gets registered anymore. As a result of that ajax validation of course cannot do its work. But even registering these scripts manually doesn’t work.

The array “attributes” in run function of CActiveForm (which is called by CForm as far as I can see) is filled with different values depending on showErrorSummary being turned on or of. But I really can’t find a reason for that.

I’m really getting a bit desperate because I would love to use formbuilder. It saves a lot of code writing and is so easy to administrate (once it works ;o)).

Hooray, I solved it! I found this line in the render function of yii.framework.web.form.CFormInputElement:




'{error}'=>$this->getParent()->showErrorSummary ? '' : $this->renderError(),



This obviosly prevents rendering errors when showErrorSummary is set to true. I wonder, if there is a sense in that which I don’t get? Could anybody explain if so? Or is it a bug?

This is how I solved this problem:

I created the following class in my components folder.




class FormInputElement extends CFormInputElement {

    public function render() {

        if($this->type==='hidden')

	    return $this->renderInput();

        $output=array(

	    '{label}'=>$this->renderLabel(),

	    '{input}'=>$this->renderInput(),

	    '{hint}'=>$this->renderHint(),

	    '{error}'=>$this->renderError(), // this is the line I changed

        );

        return strtr($this->layout,$output);

    }

}



In the form config file I put:




return array(

    'activeForm'=>array(

        'enableAjaxValidation'=>true,

        'id'=>'house-form',

    ),

    'inputElementClass'=>'FormInputElement',

    'showErrorSummary'=>true,

    'elements'=>array(

        ...

    ),

    'buttons'=>array(

        ...

    ),

);