How To Doing Check Validation "again" Without Submit

Hi guys,

I’m practically new to Yii and am designing a survey when a radioButton must hide() or show() a div or other, keeping Ajax Validation fields always.

This from jQuery function (see below) that works correctly that clearing errors marks that Ajax Validation are displayed, when radioButton is pressed.

The problem seems that after ajax validation load once, it cannot do it again the call and is not enought clearing styles (Error classes) and perhaps should touch an ajax indicator to allowing a second validation.

Then, two question to solve:

[indent]1. [b][size="3"]Exist indicators or flags Ajax Validation?

[/size][/b]2. [size="3"]How to doing check validation again?[/size]

[/indent];) While find solution here is the jQuery code to clearing red error marks displayed from Ajax Validation:


function resetClassError($form) {

	$formA = $form;

	$formB = $formA.find('div[class="errorMessage"]');

//Clearing <li> text fields in the header list class="errorSummary"

	$formB.each(function() {

    	$formG = $(".errorSummary").find("li:contains('"+$(this).text()+"')");

    	$formG.each(function() {

        	$( this ).remove();

    	});

	});

//Clearing "error" classes for: label, textarea

	$formB.contents().filter(function(){ return this.nodeType === 3; }).remove();

	$formB.hide();

//Clearing "error" classes for: label, textarea

	$formD = $formA.find('label[class*="error"]');

	$formD.removeClass().val('');

	$formD.toggleClass('required');

	$formE = $formA.find('textarea[class*="error"]');

	$formE.removeClass().val('');

	$formE.toggleClass('required');

//Fixing tabular changes. Keeps row div intact.

	$formF = $formA.find('div[class="row error"]');

	$formF.removeClass().val('');

	$formF.toggleClass('row');

}

Hi,

Once error happens in model. It will not proceed further any process. In your case , you are clearing the error message manually. I think u missed to clear model errors :)

once you got error… what steps are you making to clear the error? that is …are you correcting error and submit once again… or what are you doing at that moment?

Hi chandran,

once get the errors if press the radioButton the red errors are clearing but are only correcting the css styles

If errors corrected means… that should call again validator. Please confirm once… model has error on it… with following code

$model->hasErrors()

validator should call again the next time that go back to moving through form using the tabular keys…

i have just debuged (on execution life) and confirm that the problem is that the second time the AjaxValidation is not shooting… as was made in first time

…all this without having to submit…

[size="2"][size="1"]

[size="2"]…so is an ajax problem…then how to do a call check validation again? [/size][/size][/size]I imagine should be activate some ajax indicator.

Hi,

please can you copy your form code

Next the form code,


<div class="form">


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

	'id' => 'form-start-form',

	'enableAjaxValidation' => true,

));

?>


<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/protected/javascript/dinamicSurvey.js',CClientScript::POS_END);?>


	<p class="note">

    	<?php echo Yii::t('app', 'Fields with'); ?> <span class="required">*</span> <?php echo Yii::t('app', 'are required'); ?>.

	</p>


    	<?php echo $form->errorSummary(array($model,$modelDetermined,$modelIndecisive)); ?>

   	

            	<!-- GENERAL FIELDS: $model - PART I -->

    	<div class="row">

                	<?php echo $form->labelEx($model,'num_days_route'); ?>

                	<?php $rangeDays = range($model::MIN_DAYS, $model::MAX_DAYS); ?>

                	<?php

                      	$rangeDaysText = array_combine($rangeDays,$rangeDays);

                      	foreach ($rangeDaysText as $key => $value)

                      	{

                          	if($key===1)

                            	$rangeDaysText[$key] = $value.' día';

                          	else

                            	$rangeDaysText[$key] = $value.' días';

                      	}

                      	$defaultValue_Num_days_route = '--Elige valor--';

                      	$options = array();

                      	if(empty($model->num_days_route))

                            	$options[$defaultValue_Num_days_route] = array('selected' => true);

                      	else

                            	$options[$model->num_days_route] = array('selected' => true);

                	?>

                	<?php echo $form->dropDownList($model,'num_days_route',$rangeDaysText,array('empty' => $defaultValue_Num_days_route)); ?>

                	<?php echo $form->error($model,'num_days_route'); ?>

    	</div><!-- row -->

    	<div class="row">

                	<?php echo $form->labelEx($model,'how_many_people'); ?>

                	<?php $rangePerson = range($model::MIN_PEOPLE, $model::MAX_PEOPLE); ?>

                	<?php

                      	$rangePeopleText = array_combine($rangePerson,$rangePerson);

                      	foreach ($rangePeopleText as $key => $value)

                      	{

                          	if($key===1)

                            	$rangePeopleText[$key] = $value.' persona';

                          	else

                            	$rangePeopleText[$key] = $value.' personas';

                      	}

                        	$defaultHow_many_people = '--Elige valor--';

                      	$optionsTo = array();

                      	if(empty($model->how_many_people))

                            	$optionsTo[$defaultHow_many_people] = array('selected' => true);

                      	else

                            	$optionsTo[$model->how_many_people] = array('selected' => true);

                	?>

                	<?php echo $form->dropDownList($model,'how_many_people',$rangePeopleText,array('empty' => $defaultValue_Num_days_route,'options' => $optionsTo)); ?>

                	<?php echo $form->error($model,'how_many_people'); ?>

    	</div><!-- row -->

    	<div class="row">

                	<?php echo $form->labelEx($model,'amongAges'); ?>

                	<?php echo $form->labelEx($model,'from_age'); ?>

                	<?php $rangeAgeFrom = range($model::MIN_AGE, $model::MAX_AGE); ?>

                	<?php

                      	$rangeFromText = array_combine($rangeAgeFrom,$rangeAgeFrom);

                      	foreach ($rangeFromText as $key => $value)

                      	{

                          	if($key===1)

                            	$rangeFromText[$key] = $value.' año';

                          	else

                            	$rangeFromText[$key] = $value.' años';

                      	}

                      	$defaultFrom_age = '--Elige desde--';

                      	$optionsFrom = array();

                      	if(empty($model->from_age))

                            	$optionsFrom[$defaultFrom_age] = array('selected' => true);

                      	else

                            	$optionsFrom[$model->from_age] = array('selected' => true);

                	?>                                                                                                                      	

                	<?php echo $form->dropDownList($model,'from_age',$rangeFromText,array('empty' => $defaultFrom_age,'options' => $optionsFrom));?>

                	<?php echo $form->error($model,'from_age'); ?>

    	</div><!-- row -->                                                                                                                                                                                                                                                                                                     			

    	<div class="row">

                	<?php echo $form->labelEx($model,'to_age'); ?>

                	<?php

                      	if(empty($optionsFrom[$defaultFrom_age]))

                      	{

                            	$rangeAgeTo = range($model->from_age, $model::MAX_AGE);

                            	$rangeToText = array_combine($rangeAgeTo,$rangeAgeTo);

                            	foreach ($rangeToText as $key => $value)

                            	{

                                	if($key===1)

                                  	$rangeToText[$key] = $value.' día';

                                	else

                                  	$rangeToText[$key] = $value.' dias';

                            	}  

                            	$disabled = false;

                      	}else{

                            	$rangeToText = array_combine($rangeAgeFrom,$rangeAgeFrom);

                            	foreach ($rangeToText as $key => $value)

                            	{

                                	if($key===1)

                                  	$rangeToText[$key] = $value.' año';

                                	else

                                  	$rangeToText[$key] = $value.' años';

                            	}

                            	$disabled = true;

                      	}

                      	$defaultTo_age = '--Elige desde--';

                      	$optionsTo = array();

                      	if(empty($model->to_age))

                            	$optionsTo[$defaultTo_age] = array('selected' => true);

                      	else

                            	$optionsTo[$model->to_age] = array('selected' => true);


                	?>

                	<?php echo $form->dropDownList($model,'to_age',$rangeToText,array('disabled'=>$disabled, 'empty' => $defaultTo_age,'options' => $optionsTo)); ?>

                	<?php echo $form->error($model,'to_age'); ?>

    	</div><!-- row -->

    	<div class="row">

                	<?php echo $form->labelEx($model,'are_babys'); ?>

                	<?php $selectYesNo = array('1'=>'Si', '0'=>'No'); ?>

                	<?php echo $form->checkbox($model,'are_babys',$selectYesNo,array('separator'=>' ')); ?>

                	<?php echo $form->error($model,'are_babys'); ?>

    	</div><!-- row -->

    	<div class="row">

                	<?php echo $form->labelEx($model,'knowsWhere'); ?>

                	<?php $selectYesNo = array('1'=>'Si', '0'=>'No'); ?>

                	<?php echo $form->radioButtonList($model,'knowsWhere',$selectYesNo,array('separator'=>' ','class'=>'radio-knowsWhere')); ?>

                	<?php echo $form->error($model,'knowsWhere'); ?>

    	</div><!-- row -->

 			

   			

    	<!-- DETERMINED FIELDS: $modelDetermined -->

            	<div class="row" style="display:none" id="determinedFields">

                	<div class="row">

                    	<?php echo $form->labelEx($modelDetermined,'where'); ?>

                    	<?php echo $form->textArea($modelDetermined, 'where'); ?>

                    	<?php echo $form->error($modelDetermined,'where', array('validateOnChange'=>true,));  ?>

                	</div><!-- row -->

                	<div class="row">

                    	<?php echo $form->labelEx($modelDetermined,'visitSurroundings'); ?>

                    	<?php echo $form->radioButtonList($modelDetermined,'visitSurroundings',$selectYesNo,array('separator'=>' ','class'=>'radio-visitSurroundings')); ?>

                    	<?php echo $form->error($modelDetermined,'visitSurroundings'); ?>

                	</div><!-- row -->

            	</div>

   			

   			

    	<!-- INDECISIVE FIELDS: $modelIndecisive -->

            	<div class="row" style="display:none" id="indecisiveFields">

                	<div class="row">

                    	<?php echo $form->labelEx($modelIndecisive,'city_live'); ?>

                    	<?php echo $form->textArea($modelIndecisive, 'city_live'); ?>

                    	<?php echo $form->error($modelIndecisive,'city_live'); ?>

                	</div><!-- row -->

                	<div class="row">

                    	<?php echo $form->labelEx($modelIndecisive,'farAway'); ?>

                    	<?php echo $form->radioButtonList($modelIndecisive,'farAway',$selectYesNo,array('separator'=>' ','class'=>'radio-farAway')); ?>

                    	<?php echo $form->error($modelIndecisive,'farAway'); ?>

                	</div><!-- row -->

                	<div class="row">

                	<!-- INI-Cabecera de la Pregunta -->

                    	<?php echo $form->labelEx($modelIndecisive,'preferences'); ?>

                    	<!-- FIN-Cabecera de la Pregunta -->

                    	<?php echo $form->labelEx($modelIndecisive,'extra_preference'); ?>

                    	<?php echo $form->textArea($modelIndecisive, 'extra_preference'); ?>

                    	<?php echo $form->error($modelIndecisive,'extra_preference'); ?>

                	</div><!-- row -->

                	<div class="row">

                    	<?php echo $form->labelEx($modelIndecisive,'extra_NoPreference'); ?>

                    	<?php echo $form->textArea($modelIndecisive, 'extra_NoPreference'); ?>

                    	<?php echo $form->error($modelIndecisive,'extra_NoPreference'); ?>

                	</div><!-- row -->

            	</div><!-- row -->

   			

<?php

echo GxHtml::submitButton(Yii::t('app', 'Save'));

$this->endWidget();

?>

</div><!-- form -->

i have just find the indicator that solve the problem: forceValidate, is at jquery.yiiactiveform.js.

i probe to change flag indicator forceValidate in live execution and works fine!

Now, if i need to access at forceValidate variable to changing the value to true, this solve the problem, but the difficulty that is an external variable of jquery.yiiactiveform.js…

[size="3"]does anyone know where to locate forceValidate variable?[/size]

jquery.yiiactiveform.js code


(function ($) {

	/*

 	* returns the value of the CActiveForm input field

 	* performs additional checks to get proper values for checkbox / radiobutton / checkBoxList / radioButtonList

 	* @param o object the jQuery object of the input element

 	*/

    	.

    	.

    	.

	/**

 	* yiiactiveform set function.

 	* @param options map settings for the active form plugin. Please see {@link CActiveForm::options} for availablel options.

 	*/

	$.fn.yiiactiveform = function (options) {

    	.

    	.

    	.

        	settings.submitting = false;  // whether it is waiting for ajax submission result

        	var validate = function (attribute, forceValidate) {

            	if (forceValidate) {

                	attribute.status = 2;

            	}

            	$.each(settings.attributes, function () {

                	if (this.value !== getAFValue($form.find('#' + this.inputID))) {

                    	this.status = 2;

                    	forceValidate = true;

                	}

            	});

            	if (!forceValidate) {

                	return;

            	}

    	.

    	.

    	.

Solved!

It is somewhat abrupt because is forcing to validating each field at all times, but even so does not involve problems.

Ideally, should have changed the variable from another file and so avoid always to calling validation.

I hope it is useful:

jquery.yiiactiveform.js code

[size="1"]Note: changes marked with INI-A079.[/size]


(function ($) {

    	/*

    	* returns the value of the CActiveForm input field

    	* performs additional checks to get proper values for checkbox / radiobutton / checkBoxList / radioButtonList

    	* @param o object the jQuery object of the input element

    	*/

	var getAFValue = function (o) {

		var type,

			c = [];

		if (!o.length) {

			return undefined;

		}

		if (o[0].tagName.toLowerCase() === 'span') {

			o.find(':checked').each(function () {

				c.push(this.value);

			});

			return c.join(',');

		}

		type = o.attr('type');

		if (type === 'checkbox' || type === 'radio') {

			return o.filter(':checked').val();

		} else {

			return o.val();

		}

	};

//INI-A079. New.

    	var activateValidation = true;

    	

    	function setActivateValidation(value) {

        	activateValidation = value;

    	}

//FIN-A079.


    	/**

    	* yiiactiveform set function.

    	* @param options map settings for the active form plugin. Please see {@link CActiveForm::options} for availablel options.

    	*/

    	$.fn.yiiactiveform = function (options) {

    	.

    	.

    	.

			settings.submitting = false;  // whether it is waiting for ajax submission result

			var validate = function (attribute, forceValidate) {

//INI-A079. New.

				if(activateValidation){

					forceValidate = activateValidation;

				}

//FIN-A079.

				if (forceValidate) {

					attribute.status = 2;

				}

				$.each(settings.attributes, function () {

					if (this.value !== getAFValue($form.find('#' + this.inputID))) {

						this.status = 2;

						forceValidate = true;

					}

				});

				if (!forceValidate) {

					return;

				}

    	.

    	.

    	.