Bypassing form validation

Tom0 06 Apr, 2009
Hi,

I have got my form quite happily doing two different things when users click one of two buttons (with name="formtask"): "Save" and "Submit" - one just saves the progress and the other saves and makes the form readonly from then on. (see my previous post, http://www.chronoengine.com/forums/index.php?option=com_chronoforums&cont=posts&f=2&t=12334).

But what I want now is for users to be able to save the form even if they havent completed all of the required fields - hence bypassing the validation but ONLY when users click "Save".

I have tried a few things and I think the closest is something like: (in mooValidation.js - line 44)

onSubmit :  function(ev){
		if(this.form.formtask.value=="Save"){} else {; 
		if(!this.validate()) new Event(ev).stop() };
	},


but this doesnt work either...

If anyone has any ideas I would be very grateful to hear them!

Thanks,
Tom
Tom0 06 Apr, 2009
Ah not a problem! Stick this in the page somewhere:

<script type="text/javascript">
	function bypassValidation() {
		document.myformname.removeEvents('submit');
	}
</script>


and add
onclick="bypassValidation()"
to the "Save" button! Genius!
GreyHead 06 Apr, 2009
Hi Tom0,

Excellent - well done !!

Here's a lightly neater version of the code that will load the snippet in the header and apply it to the save button:
<?php
$doc =& JFactory::getDocument();
$script = "
  window.addEvent('domready'. function() {
    $('save').addEvent('click', bypassValidation() {
      document.myformname.removeEvents('submit');
    });
  }); 
";
$doc->addScriptDeclaration($script);
?>
Not checked and may need debugging

NB requires an id='save' in the save button

Bob
Tom0 07 Apr, 2009
Thanks Bob! That seems to work just as well - one (maybe ignorant) question though - why is this a preferable method?

Tom
GreyHead 07 Apr, 2009
Hi Tom0,

Just slightly better pratice; the script is added in the header instead of in the body; and any execution is delayed until the page is loaded. This helps avoid some errors in IE with the Operation Aborted, and some JavaScript errors when scripts look for html that hasn't loaded yet.

That said your script would probably run without problem - I just try to encourage the better practice.

Off topic a little but my current working style with script snippets is like this:
<?php
$doc =& JFactory::getDocument();
$script  = "";
. . .
$script .= //some snippet
. . .

$script .= // some other snippet
. . .
if ($script) {
  $script = "window.addEvent('domready', function() { $script });";
  $doc->addScriptDeclaration($script);
}
?>

You have to watch the quotes a bit carefully but this works well - any of the snippets can be customised using PHP variables and yet they all end up in a single JavaScript block in the page header.

Bob

PS The 'domready' event is specific to MooTools so the library needs to be loaded; I think 'onload' would work but delays the loading until all the assests, images, etc, are complete whilstl domready only waits for the html to load.
Tom0 08 Apr, 2009
Awesome. I have found it difficult to get best practise information like this and often google-searches dont compare advantageous of different methods/styles. Really good to have such excellent support and advice here even when Im doing something not really in the remit of chrono forms so thanks a million!😀
This topic is locked and no more replies can be posted.