My mission is to make a field a required field, but if a ajax/remote call returns a false then to cancel that required field order. I have been playing about for days now trying to solve this.
I have tried using depends in many different ways, and initially used this example but could not get it to work - https://github.com/jzaefferer/jquery-validation/issues/431
(that example won't even try and look at the remote code for some reason)
Been trying to strip back my code just to gain an understanding of how depends works.
HTML:
<form method="post" action="" id="register_employee_form" >
<table>
<tr>
<tr>
<td class="column_1"><label>FORENAME</label></td>
<td><input name="forename" id="forename" value="" /></td>
</tr>
</table>
<input type="submit" value="Register" onclick="register(); return false;" />
</form>
JQUERY:
function register()
{
var cancel_validation = 0; // Dont cancel validation
var form = $("#register_employee_form");
var validator = form.validate({
errorElement: "p",
rules:
{
forename:
{
required: true,
remote: {
url: "ajax_code.php",
complete: function(response) {
if (response.responseText == "false")
{
cancel_validation = 1;
}
} // complete
}, // remote
depends: function() {
if(cancel_validation == 1)
{
return false;
}
}
} // forename
},
messages: {
forename: {
required : "forename required",
remote : "REMOTE"
}
},
submitHandler: function(form) {
form.submit();
}
});
validator.form();
}
ajax_code.php: For now just a simple echo of false or true for testing
<?php
echo "true"; // return false if OK
?>
So if the remote response returns a false, then no validation is done, but if it returns a true then validation is done.
Hope this make some sense. Quite hard getting across what I am trying to achieve and what I have tried so far.
Any ideas and help gratefully received.