resourcer icon indicating copy to clipboard operation
resourcer copied to clipboard

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

Open Marak opened this issue 14 years ago • 1 comments

     this.property('to', 'string', {
       required: true,
       message: "The to field is required to send an email."
     });

Marak avatar Dec 23 '10 03:12 Marak

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;
    }
});

janpieper avatar Feb 20 '11 13:02 janpieper