awa.mvc.ModelRules.ModelRule Class
/var/www/azaleahealth.com/branches/docs/source_repo/awa/resources/apps/js/awa/mvc/ModelRules.js:8
Not really a class but just an outline for how a ModelRule should be witten. Model rules are a set of predefined rules that can be used with models.
Creating a rule:
//rule key. should be unique
required: {
//the default message displayed when the rule test failes
errorMessage: "This field is required.",
//error message can also be a function that returns the description
//any properties in the rule definition of the attributeProps is passed as the params
//model and attributes are also attached to params
errorMessage: function(value, attr, params){
return params.model.getName(attr)+" is required.";
},
//This is the test method. It should return false if the the test fails.
//any properties in the rule definition of the attributeProps is passed as the params
//model and attributes are also attached to params
method: function (value, attr, params) {
return value ? value != '' : false;
}
},
These should coincide with the rule types that the backend support for validation before hitting the several.
All rules other than required are considered optional. Meaning that if no value is submitted (empty string, undefined) the rule is not checked.
pat_dob: {
number: true
}
//the above will fail if pat_dob is set to "string" but pass on "56", 56, or ""
pat_dob: {
number: true,
required: true
}
//the above will fail if pat_dob is set to "string" and "" but pass on "56", or 56
You may also override the error message in the config
pat_dob: {
required: {
errorMessage: "Quit being lazy and ask the patient when they were born"
}
}
All rules with the exception of custom are interchangeable with the backend. "custom " rules require a function to validate.
some rules (i.e. unique) cannot be properly validated by the front end.
Rules can be defined in the attributeProps of a model or can be set after the model is instantiated.
Methods
method
-
value -
[attr] -
[params] -
[params][model]
Parameters:
-
valueObject -
[attr]String optional -
[params]Object optional -
[params][model]Object optional
Returns:
Returns true if passes validation
Properties
errorMessage
String
User readable validation. Can also be a function. Receives same params as method