jpillora / verifyjs

Verify.js - A powerful, customizable asynchronous validation library
http://verifyjs.jpillora.com/
161 stars 61 forks source link

Group Rules #6

Open stemo40 opened 10 years ago

stemo40 commented 10 years ago

First, I'd like to say that this is beautifully done!

Second, I have a situation where I need to combine two form elements together so that if the first form element (textfield) is not empty then the second form element (select) needs to have a value too. I can't seem to figure out how to make that happen. I have several combinations of this type in the form, so is there anyway to do this?

jpillora commented 10 years ago

Hi Steve,

Thanks! I haven't looked at this project in quite some time, though here is the code for the date range validator in the Group Rules example:

$.verify.addGroupRules({
  dateRange: function(r) {
    var start = r.field("start"),
        end = r.field("end");

    if(start.length === 0 || end.length === 0) {
      r.warn("Missing dateRange fields, skipping...");
      return true;
    }

    var startDate = $.verify.utils.parseDate(start.val());
    if(!startDate)
      return "Invalid Start Date";

    var endDate = $.verify.utils.parseDate(end.val());
    if(!endDate)
      return "Invalid End Date";

    if(startDate >= endDate)
      return "Start Date must come before End Date";

    return true;
  }
});

which has the html:

<form>
  <input value="3/1/2x13" data-validate="date,dateRange#start">
  <input value="2/1/2013" data-validate="date,dateRange#end">
  <input type="submit" class="submit">
</form>

so yeah, it'll need to be changed for your use case, though let me know how that goes

stemo40 commented 10 years ago

Jaime,

Thanks for the quick reply! I saw that code and tried to adopt it like this:

$.verify.addGroupRules({ combo: function(r) { var a = r.field("a"), b = r.field("b"); if(a.val() != "" && b.val() == "") { return "Please indicate a unit"; return true; }
} });

HTML:

When I use this code, nothing happens on blur, on submit the "measure" textfield shows the error that it is required.

jpillora commented 10 years ago

So I had a look into this, it seems that there's a bug with group rules, I probably won't have time to fix it anytime soon, though here's a temporary workaround: http://jsfiddle.net/jpillora/R4t84/1/

stemo40 commented 10 years ago

Thank you! I should be able to use this.