0

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.

  • 1
    Show a more complete code example. I don't see the `remote` rule declared anywhere in your code. I don't see your HTML markup. I also cannot see the server-side function you're calling with `remote`. And if you tried the suggested workaround from the developer, show us _exactly how_ **you** implemented it. – Sparky Aug 19 '14 at 14:59
  • there is a simple question: why would you wanna do that? whats the advantage of that? you can simply add a required class to the fields which need to be validated - I don't see the reason why you would wanna make an additional request. using an ajax request for validation isn't very userfriendly - you could use a normal backend validation for that – gulty Aug 19 '14 at 15:03
  • @gulty, the field is not mandatory, however **if** it's filled out, the value must to be compared with something on the server (`remote`). That seems like a pretty standard thing to me. – Sparky Aug 19 '14 at 15:05
  • sure thing - but using an ajax request doesnt really make sense to me. no js is needed for this kind of serverside validation. the request is sent either way – gulty Aug 19 '14 at 15:07
  • @gulty, the `remote` method is part of this client-side plugin and it works fine. Starting a debate about client-side versus server-side validation is not appropriate for the comments section. We _should_ already understand the basic differences and focus on answering the specific question. – Sparky Aug 19 '14 at 15:11
  • OP, the syntax of your first code block is completely wrong. Showing us "irrelevant" code to demonstrate that you "played around" is not really helpful. You're better off showing us the _relevant_ code as I requested in my first comment. – Sparky Aug 19 '14 at 15:13
  • I have now change the post to try and show you the actual code that I am trying to use. The reason for the requirement is, I want the field to be mandatory, but I want the ajax to check to see if there is certain data in the database. If there is no data then I want to allow nothing to be entered. i.e. cancel the validation. – Oliver Tsang Aug 19 '14 at 15:17
  • One huge problem here is that you've wrapped `.validate()` within a function that you call from an inline `click` handler. **You do not need a `click` handler AND `.validate()` should only be called _once_ within a DOM ready event handler.** The `.validate()` method is the _initialization_ method of the plugin, not the method of testing your form. Once properly initialized, testing the form is automatic. – Sparky Aug 19 '14 at 15:17
  • Sparky, I implemented the suggested workaround exactly as he showed, but with my own url. It just didn't work. It didn't even try and look at the remote code. To me, it should have worked but it just didn't. The developer there does suggest that a fix to .validate could null and void his work around so that shed some doubt over the code. – Oliver Tsang Aug 19 '14 at 15:20
  • Spraky. I did initially have it in a $( document ).ready(function() {} but for some reason I put it on a onclick. I cannot recall why I tried that now, but yes I agree with you. – Oliver Tsang Aug 19 '14 at 15:22
  • I see no need for a `complete` option within `remote`. This is something the plugin handles internally. – Sparky Aug 19 '14 at 15:36
  • @Sparky. I initially didn't have that either. That was suggested to me to use that when I asked for an answer regarding this issue previously. Is what I am trying to do even possible or shall I find a work around? Appreciate your input by the way :-) – Oliver Tsang Aug 19 '14 at 15:46
  • Way too many non-standard things going on here. Looks like a combination of issues. Since I don't feel like setting up a server-side script for testing this out, I'm probably not going to attempt to solve it. However, the code you're showing us here is nowhere near the workaround proposed by the developer. – Sparky Aug 19 '14 at 15:50
  • I also noticed the workaround is not quite the same as what you're requesting. You want `required` to depend on `remote`, where in the workaround, `remote` depends on `required`. – Sparky Aug 19 '14 at 15:52
  • You said you want `required` to "depend on a response from remote"... but you never said what response. `remote` only passes validation when the response is `true`... that is the only condition for passing. ANY other response will trigger a validation message. So if `remote` fails validation, it doesn't even matter if the field is required or not. – Sparky Aug 19 '14 at 15:55
  • Thanks Sparky. Sorry, wasn't ignoring your replies, I left my office and now looking at home. I'll take on board what you have said and try some more stuff out. – Oliver Tsang Aug 19 '14 at 18:58
  • p.s. this was the original post I put up about this issue. Would be interest to hear your thoughts on the responses I got. http://stackoverflow.com/questions/24891764/validate-cancel-the-validation-for-a-field-based-on-remote-response – Oliver Tsang Aug 19 '14 at 19:00
  • @Sparky. Are you saying that I should just use the remote call to decide if the field is valid or not? The trouble is, if the user enters nothing then remote is not actioned (in my example anyway). I think that is why I put it on a OnClick, to try and force remote to be called, but that didn't work anyway. Is there a way of getting remote to be called if the user doesn't enter anything in the field? – Oliver Tsang Aug 19 '14 at 19:05
  • If you implement the Validate plugin correctly, all validation rules will be evaluated when the `submit` button is clicked. However, you've broken that default feature by not initializing the plugin properly. Additionally, the `remote` rule will never be triggered if the field is left blank. I cannot be more specific because I'd have to practically rewrite your code from scratch. You also never explained the point of doing all this. – Sparky Aug 19 '14 at 19:08
  • Oh I thought I did above. Basically I want to set up a field that is mandatory if some entries exist in a table in a database. That's all I am trying to do. So I was making the field Required as standard initially, then trying to use Remote to check the database, and if no entries existed in the table, cancel the mandatory Requirement. – Oliver Tsang Aug 19 '14 at 19:25
  • p.s. I have now set the validate to set up when the DOM loads. – Oliver Tsang Aug 19 '14 at 19:26
  • You've already stated what you're trying to do... I get it. I am asking why you would need a field to be mandatory and then not mandatory if an entry exists in the database? <<- What is the point of this?? It's an unusual specification that I've never seen anyone ask about before. – Sparky Aug 19 '14 at 19:28
  • Ah, I see. Basically, the client want to force the user to select the manager of the employee you are entering in the form, however if this is the first employee you are entering then you have no other employees to select as the manager (as a manager is an employee). To be honest, I am going to speak to them about changing the spec because once they do start entering employees, the user will then be force to select irrelevant employees as the managers i.e. if the manager hasn't yet been entered. – Oliver Tsang Aug 19 '14 at 19:42
  • It's not been thought out too well by the client. I did start to realise it was nonsense but I was trying not to be beaten and was searching for the answer out of interest as much as anything. I appreciate your help Sparky. I have already re-written how I think it should work. Just got to convince the client tomorrow now..! – Oliver Tsang Aug 19 '14 at 19:42
  • I think it might be best for you to delete this question in that case. – Sparky Aug 19 '14 at 19:53

0 Answers0