segmentio / validate-form

Easily validate a form element against a set of rules.
43 stars 6 forks source link

Some small enhancements #10

Closed dominicbarnes closed 10 years ago

dominicbarnes commented 10 years ago

Regarding the 2nd enhancement, this allowed me to create a equal validator fn. (checking that 2 fields are equal, like confirm email/password) Without some sort of context I could hook into, I could only make this happen via closure.

I've got other ideas, and I'll likely be submitting several more PRs along the way. :)

ianstormtaylor commented 10 years ago

nice! can you show me how the equal validator works? just curious, happy to merge all this in

ianstormtaylor commented 10 years ago

0.9.0!

dominicbarnes commented 10 years ago

My equal fn looks like this:

/**
 * Validation rule that requires 1 field be equal to another
 *
 * @param {String} other  Name of 2nd field to validate against
 * @returns {Function}
 */
exports.equal = function (other) {
    var field = this;

    return function (val) {
        var form = closest(field.el, "form");
        var el = form.querySelector("[name=\"" + other + "\"]");

        return val === field.adapter.value(el);
    };
}

Pretty simple, but I didn't want to add a new dep (discore/closest) unless you wanted to. (plus I wasn't sure if you would go for the field being set as this)

Would you like me to PR equal?

ianstormtaylor commented 10 years ago

haha i wasn't sure i was gna go for it myself :) yeah would love to get that in core, equal sounds cool!