How to disable client side validation for a [cancel] button?

Hi, I’m using yii-1.1.7.r3135. I have a form like this:


<?php

$form = $this->beginWidget('CActiveForm', array(

	'id'=>'recovery-form',

   'enableClientValidation'=>true,

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

   'errorMessageCssClass'=>'clsErrorMessage',

   'htmlOptions'=>array('autocomplete'=>'off'),

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

  	'errorCssClass'=>'clsError',

  	'successCssClass'=>'clsSuccess',

  	'validatingCssClass'=>'clsValidating',

	),

));

?>   

And then I have these two buttons:




<?php

   echo CHtml::submitButton(Yii::t('Yii', 'Send'), array('name'=>'ok', 'class'=>'clsPrimaryButton'));

   echo CHtml::submitButton(Yii::t('Yii', 'Cancel'), array('name'=>'cancel', 'class'=>'clsSecondaryButton'));

?>

How can I disable client side validation when the Cancel button is pressed? The idea is to redirect to another page when this button is pressed, which I do in the action with this code:




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

	$this->controller->redirect(array('login/'));

}

But since I also need client side-validation, the form is never submitted when Cancel is pressed. Of course this simple reditect could be done with a link, but if the cancelation involves other logic (deleting something in session or user state, for instance) then a link would not work.

Note1: in prado this was done with CausesValidation="false" for the button.

Note2: due to graphic design and management issues, I would prefer not to be forced to use a simple CHtml::link for the "cancel".

Thanks.

I’m using ‘beforeValidate’ clientOption. Like this …




$form = $this->beginWidget('CActiveForm',

	array(

		'id'=>'holiday-form',

		'enableAjaxValidation' => true,

		'focus' => '#Holiday_year',

		'clientOptions' => array(

			'validateOnSubmit' => true,

			'beforeValidate' => 'js:beforeValidate',

		),

	)

);

$js = "function beforeValidate(form) {

-   if ( form.data('submitObject') {

+   if ( form.data('submitObject') ) {

        this.validateOnSubmit = false;

        this.beforeValidate = '';

        form.submit();

        return false;

    }

    return true;

}";

Yii::app()->clientScript->registerScript('editprofile-form-beforeValidate', $js);



It’s working for me anyway, but I don’t know whether it’s quite good or not, because I couldn’t get an answer when I asked a question like yours some time ago. :(

And I’d like my solution to be reviewed by experienced fellows. :P

[EDIT]

The code had an apparent syntax error.

I missed the beforeValidate option. But I have done this using your idea:


$js = 'function beforeValidate(form) {if ( form.data("submitObject") {this.validateOnSubmit = false;this.beforeValidate = "";form.submit();return false;}return true;}';


$form = $this->beginWidget('CActiveForm', array(

	'id'=>'recovery-form',

   'enableClientValidation'=>true,

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

   'errorMessageCssClass'=>'clsErrorMessage',

   'htmlOptions'=>array('autocomplete'=>'off'),

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

  	'errorCssClass'=>'clsError',

  	'successCssClass'=>'clsSuccess',

  	'validatingCssClass'=>'clsValidating',

  	'beforeValidate'=>"js:'{$js}'",

	),

));

It seems to work fine, but I feel it too complex. It’d be better if the CActiveForm had an option to indicate which buttons need to have validation and which not.

Puf, I did not notice that this causes a js error, because I tested in Firefox. But in MSIE the error is reported with a popup dialog and the form can not be submitted:

http://www.yiiframework.com/forum/index.php?showtopic=22936

Firefox of couse doesn’t mind the error and continues submitting the form, which in this case might not be the right behavior.

Added a feature request: http://www.yiiframework.com/forum/index.php?/topic/22979-avoiding-client-side-validation-on-secondary-submit-buttons/

I avoided the client side validation, because of this problem. There should be a proper fix for this built into Yii.