How do you specify custom error messages when validate() fails? #13

Open
Marak opened this Issue Dec 23, 2010 · 1 comment

Projects

None yet

2 participants

@Marak
Marak commented Dec 23, 2010
     this.property('to', 'string', {
       required: true,
       message: "The to field is required to send an email."
     });
@janpieper

would be great to be able to specify more than one error message because in most cases there isn't only one validation rule.

this.property("password", "string", {
    assert: function(val) {
        if (val.length <= 6 || val.length => 30) {
            return false; // validation failed due to invalid length
        } else if (val.match(/^[a-z0-9_]+$/) != val) {
            return false; // not allowed characters in use
        }
        return true;
    }
});

or

this.property("password", "string", {
    assert: function(val) {
        var errors = [];
        if (val.length <= 6 || val.length => 30) {
            errors[errors.length] = "validation failed due to invalid length";
        }
        if (val.match(/^[a-z0-9_]+$/) != val) {
            errors[errors.length] = "not allowed characters in use";
        }
        return errors.length == 0;
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment