Skip to content

Commit 2e593dc

Browse files
committed
Bugfix and docs for validationMessage
1 parent ad48da7 commit 2e593dc

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,29 @@ General options most field types can handle:
171171
title: "Street", //Title of field, taken from schema if available
172172
notitle: false, //Set to true to hide title
173173
description: "Street name", //A description, taken from schema if available
174+
validationMessage: "Oh noes, please write a proper address" //A custom validation error message
175+
}
176+
```
177+
178+
Validation Messages
179+
-------------------
180+
Per default all error messages but "Required" comes from the schema validator
181+
[tv4](https://github.com/geraintluff/tv4), this might or might not work for you.
182+
If you supply a ´´´validationMessage´´´ proṕerty in the form definition, and if its value is a
183+
string that will be used instead on any validation error.
184+
185+
If you need more fine grained control you can supply an object instead with keys matching the error
186+
codes of [tv4](https://github.com/geraintluff/tv4). See ```tv4.errorCodes```
187+
188+
Ex.
189+
```javascript
190+
{
191+
key: "address.street",
192+
validationMessage: {
193+
tv4.errorCodes.STRING_LENGTH_SHORT: "Address is too short, man.",
194+
"default": "Just write a proper address, will you?", //Special catch all error message
195+
"required": "I needz an address plz" //Used for required if specified
196+
}
174197
}
175198
```
176199

src/services/decorators.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
9393
* An error can either be a schema validation message or a angular js validtion
9494
* error (i.e. required)
9595
*/
96-
scope.errorMessage = function(schemaError,$error) {
96+
scope.errorMessage = function(schemaError) {
9797
//User has supplied validation messages
9898
if (scope.form.validationMessage) {
9999
if (schemaError) {
100100
if (angular.isString(scope.form.validationMessage)) {
101101
return scope.form.validationMessage;
102102
}
103103

104-
return scope.form.validationMessage[schemaError.code] || scope.form.validationMessage.default;
104+
return scope.form.validationMessage[schemaError.code] || scope.form.validationMessage['default'];
105105
} else {
106-
return scope.form.validationMessage.required || scope.form.validationMessage;
106+
return scope.form.validationMessage.required || scope.form.validationMessage['default'] || scope.form.validationMessage;
107107
}
108108
}
109109

0 commit comments

Comments
 (0)