beforValidate not working anymore to prevent validateOnSubmit?

That works for cancel but as I stated above, “Cancel” is just a label, the action could be anything from “find postal code” to “delete”. It’s like using Prado’s [font=“Courier New”]OnCommand[/font].

For instance, I have a form which allows the user to enter his e-mail alias for a Postfix e-mail system with MySQL backend. But he can also delete the alias from the same page. I have not been able to find a “light” alternative to having two buttons each one using the same action which renders the page. An alternative could be a CHtml::Button submitting to another action which delete the alias and then have that action redirecting the browser to the original action, but that seems a bit too much to me, since there’s a get for the delete and then another get for the rendering, instead of just a single post.

I would just add a ‘remove’ icon (button) which deletes it with an Ajax request. Very much like theYii grid.

Then rename the label for the submit button to ‘Update’.

Adding an additional ‘Cancel’ button is IMO good interface design.

But that would solve the issue?

In my book, either you submit the form, or you don’t…

It could work with Ajax, but it seems to me kind of an overkill for something so simple, and it becomes overcomplex for setting flashes, updating the page and reporting errors.

I have ended up with a modification in "jquery.yiiactiveform.js".




....

if (settings.validateOnSubmit) {

	$form.find(':submit').live('mouseup keyup',function(){

		$form.data('submitObject',$(this));

	});

	var validated = false;

	$form.submit(function(){

		if (validated)

			return true;

+		if (settings.skipValidate && settings.skipValidate($form))

+			return true;

		if(settings.timer!=undefined) {

			clearTimeout(settings.timer);

		}

		submitting=true;

		if(settings.beforeValidate==undefined || settings.beforeValidate($form)) {

....



And in my _form.php:




....

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

	array(

		'id'=>'some-form',

		'enableAjaxValidation' => true,

		'clientOptions' => array(

			'validateOnSubmit' => true,

			'skipValidate' => 'js:skipValidate',

		),

	)

);

$js = <<< EOS

function skipValidate(form) {

	var button = form.data('submitObject');

	if ( button && button[0].id == 'cancel_btn' ) {

		return true;

	}

	return false;

}

EOS;

Yii::app()->clientScript->registerScript('somemodel-someform-skipValidate', $js);

....



When 1) "skipValidate" is defined and 2) it has returned true, then form will be submitted without validation.

I know it’s not a good practice to alter the core code. But I couldn’t find a simple solution other than this.

I have setup my script mapping so that ‘jquery.yiiactiveform.js’ refers to the modified version.