title: jquery - Choosing a validation message during validation
body: <p>I'm building a page that renders a series of questions to the page, some of these questions have multiple options other might just require the user to fill in someting in a text box. </p>
  
  <p>Because of the nature of these questions and the way they need to be validated (the whole question needs to be looked at) I came to the conclusion that the out of the box required field validator wouldn't quite cut it.</p>
  
  <p>So I started writing my own and hit a bit of a hurdle ...</p>
  
  <p>In my validation method I pass in 2 possible "error messages" that might occur based on conditions in the validator. </p>
  
  <p>How do I get them in the right place in this context?
  On the basis that I don't know what the actual error message should be until i get to the failing condition in the validation handler function i need to be able to say to jquery "set this as the error message and fail this field".</p>
  
  <pre><code>$.validator.unobtrusive.adapters.add("myrule", ['enable', 'err1', 'err2'], function (options) {
      var value = {
          enable: options.params.enable,
          err1: options.params.err1,
          err2: options.params.err2
      };
      options.rules["myrule"] = value;
      if (options.message) {
          options.messages["myrule"] = options.message;
      }
  });
  
  $.validator.addMethod("myrule", function (value, element, params) {
      var question = $(element).closest('fieldset[data-questionId]');
      var result = validateMyrule(question, params['enable'], params['err1'], params['err2']);
      return result;
  });
  
  function validateMyrule(question, enabled, err1, err2) 
  {
       // actual code omitted to simplify question
  
       if(someCondition)
       {
           if(someCondition)
           {
               errorMessage = err1;
           }
           else
           {
               errorMessage = err2;
           }
  
           return false;
       }
  
       return true; 
  }
  </code></pre>