Yii Framework Forum: Formbuilder. Ajaxvalidate Doesn't Work With Showerrorsummar Switched To Truey - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Formbuilder. Ajaxvalidate Doesn't Work With Showerrorsummar Switched To Truey Rate Topic: -----

#1 User is offline   Anni_g 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 18-March 13
  • Location:Berlin, Germany

Posted 21 March 2013 - 06:39 AM

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(); 

0

#2 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 21 March 2013 - 06:44 AM

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',
	), 

Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#3 User is offline   Anni_g 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 18-March 13
  • Location:Berlin, Germany

Posted 21 March 2013 - 06:58 AM

Awesome! That works for me. Thanks a lot!
0

#4 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 21 March 2013 - 07:04 AM

Welcome !!!
Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#5 User is offline   Anni_g 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 18-March 13
  • Location:Berlin, Germany

Posted 21 March 2013 - 07:32 AM

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.
0

#6 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 21 March 2013 - 07:45 AM

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
Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#7 User is offline   Anni_g 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 18-March 13
  • Location:Berlin, Germany

Posted 21 March 2013 - 11:27 AM

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.
0

#8 User is offline   Anni_g 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 18-March 13
  • Location:Berlin, Germany

Posted 22 March 2013 - 10:40 AM

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)).
0

#9 User is offline   Anni_g 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 18-March 13
  • Location:Berlin, Germany

Posted 23 March 2013 - 04:43 AM

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(
        ...
    ),
);

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users