Yii Framework Forum: Postpone Clientvalidation Until After Initial Submit - Yii Framework Forum

Jump to content

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

Postpone Clientvalidation Until After Initial Submit Rate Topic: -----

#1 User is offline   JustinV 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 08-February 12

Posted 16 December 2012 - 05:02 PM

I not yet been able to find what I am hoping is a simple solution to my question. Here is the desired behavior.
  • No validation is performed until user clicks submit
  • Once user clicks submit, the form is validated and errors are displayed
  • After validation, errors are removed from fields as user enters data


I have been able to get each piece of this, but not all at once.

If I set enableClientValidation set to false, no errors are shown until the user clicks submit (which is what I want) but once the errors are shown they do not disappear until submit is clicked again.

If I set enableClientValidation set to true, errors are shown for invalid data as soon as the user leaves the field (which is what I want to avoid), but after submit the errors disappear for fields as soon as they are valid.

My theory is that when completing a form, the user may bounce between fields before submitted. They may return to a field after skipping it and I don't want to show errors at that point. Once they click submit, then I would like to show any errors that they have not corrected and have those errors disappear as soon as the data is corrected.

Is this possible or is it best just to stick with enableClientValidation set to true and let errors display even though the user isn't done filling out the form?
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 18 December 2012 - 10:00 AM

Dear Justin

I hope the following serves the purpose.

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'comment-form',
	'enableClientValidation'=>$model->hasErrors()?true:false,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
)); ?>

So we are going to show validation errors, only after the the model has filled with error(s).
This is going to happen only after the form submission.
It has acheived almost 90% percent of our requirement.
The only aberrent is label looks red even validation succeeds.

To circumvent this, I have done the following.
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'comment-form',
	//'enableAjaxValidation'=>$model->hasErrors()?true:false,
	'enableClientValidation'=>$model->hasErrors()?true:false,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
		'afterValidateAttribute'=>'js:function(form, attribute, data, hasError)
		{   var id=attribute.id;
		    if(!hasError)
			$("label[for="+id+"]").removeClass("error required");
		}'
	),
)); ?>

It works well even if we enable ajaxValidation.

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