Hi, I have this code binded to the clientOptions.beforeValidate in an CActiveForm configure with clientValidation, in order to prevent validation when a secondary button is pressed:
function beforeValidate(form) {
if (form.data('submitObject') && form.data('submitObject')[0].name == 'cancel') {
this.validateOnSubmit = false;
this.validateOnChange = false;
this.beforeValidate = ''; // the problem is caused by this line
form.submit();
return false;
}
else {
return true;
}
}
Thanks, but the problem is not in the PHP code but in the Javascript code. The [font="Arial"]code that was working is exactly the one I posted. The[/font] objetive of that code is to avoid client (javascript) validation on form submit, so I can have two submit buttons in the same CActiveForm, for instance, one for save and one for cancel, and in case the cancel button is pressed, no validation is triggered on submit. That would be similar to the [font="Courier New"]CausesValidation [/font]switch in Prado.
Is there any way to emulate this [font="Courier New"]CausesValidation="false" [/font]for client side validation?
P.S. for server side validation there’s no problem since I check $_POST[‘cancel’] to see if the cancel but was pressed.
P.P.P.S. now that I remember, even the original code could be causing the problem, since I didn’t tried the page with MSIE before (and MSIE is more strict when reporting JS errors)
Are you 100% sure? Have you tried with MSIE with js debugging turned on? I have the same code and the error is thrown on submit, because [font=“Courier New”]this.beforeValis a js error in Yii’s validation code, no further js is executed and the form is sent without validation.
I correct myself. Doing this will result in the beforeValidate function being called an infinitum. In Firefox the execution will halt and the form will be submitted, in MSIE the recursion will freeze the browser. So, don’t do it
It just checks whether the submission (and validation) is caused by clicking any button. When you hit Enter key, “form.data(‘submitObject’)” returns undefined.
And the code of the previous post has an error, too.
Try this code, it should work, I added this code directly to CActiveForm as a patch, but I still need to fix it since the function name "beforeValidate": could clash with another existing function:
function beforeValidate(form) {
if (form.data('submitObject')) {
if (form.data('submitObject')[0].name in {$buttons}) {
this.validateOnSubmit = false;
this.beforeValidate = function beforeValidate(form) { form.submit(); };
form.submit();
return false;
}
}
return true;
}
Also this.[font="Courier New"]beforeValidate = function beforeValidate(form) { form.submit(); };[/font] is a very dirty hack, but without something like that the whole thing will fail.
But why to you submit again the form from the submit handler ?
The beforeSubmit function is expected to return true or false… so you just test if the button pressed is your CANCEL button and in that case return false…
Yes, canceling of the form input can be achieved by a simple link.
Or, further more, some users may not need a cancel link at all. They can just go back where they have been. … just as we see in the post form of this forum.
I agree that there’s no technical reason to implement the canceling by submitting.
But I want a pair of buttons side by side in the last row of my form: "Submit" (or "OK", "Send", "Update", …etc) and "Cancel". The two buttons should look equally important to the user. I want to let the users know explicitly that they can cancel the form.
[EDIT]
Maybe this is due to the fact that I’ve been working with the Windows modal dialogs for a very long time.
"Cancel" is just a label. It could be anything, for example "Delete", "Find postal code", etc. which launches a server side action, like the [font="Courier New"]OnCommand[/font] handler in Prado.