-
-
Save mattlevine/73efd4549d91f2a0a4b236535ce48730 to your computer and use it in GitHub Desktop.
Mura: How to add a validation method to a custom form that doesn't override Mura's async form submission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- Vanilla js ---> | |
<script> | |
Mura(function(m) { | |
window.customFormValidation = function(theform) { | |
// example validation | |
if (theform.somefield.value === '') { | |
alert('Please enter a value in somefield'); | |
theform.somefield.focus(); | |
return false; | |
} | |
// let Mura process the form now | |
Mura.submitForm(theform); | |
return false; | |
} | |
}); | |
</script> | |
<form method="post" name="someform" onsubmit="window.customFormValidation(this);return false;" novalidate> | |
<input name="somefield" /> | |
<input name="submit" type="submit" value="Submit" /> | |
</form> | |
<!--- Example using jQuery Validation ---> | |
<cfif len(m.event('name'))> | |
<!--- Do stuff with submitted data ---> | |
<h3>Thanks!</h3> | |
<cfelse> | |
<script> | |
Mura(function(){ | |
//if jquery.validate is already loaded you don't need to wrap it in a Mura.loader() method | |
Mura.loader().loadjs( | |
'https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js', | |
function(){ | |
$('#myform').validate({ | |
submitHandler:function(frm){ | |
Mura.submitForm(frm); | |
} | |
}); | |
} | |
); | |
}) | |
</script> | |
<h3>Example Form</h3> | |
<form id="myform" data-autowire=true onsubmit="return false;"> | |
name<br/> | |
<input id="name" name="name" type="text" minlength="2" required><br/> | |
<button type="submit">submit</button> | |
</form> | |
</cfif> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment