diff --git a/packages/oui-field/README.md b/packages/oui-field/README.md
index 84fc307c..52f0c5f0 100644
--- a/packages/oui-field/README.md
+++ b/packages/oui-field/README.md
@@ -30,6 +30,17 @@
name="email"
ng-model="$ctrl.user.email">
+
+
+
+
```
### Checkbox
@@ -147,9 +158,20 @@
## API
-### oui-datagrid
-
-| Attribute | Type | Binding | One-time binding | Values | Default | Description |
-| ---- | ---- | ---- | ---- | ---- | ---- | ---- |
-| `help-text` | string | @? | yes | | | the field label |
-| `label` | function | @? | yes | | | a text to help fill the form field |
+| Attribute | Type | Binding | One-time binding | Values | Default | Description |
+| ---- | ---- | ---- | ---- | ---- | ---- | ---- |
+| `error-messages` | object | | no | | | a dictionary to override default messages |
+| `help-text` | string | @? | yes | | | the field label |
+| `label` | function | @? | yes | | | a text to help fill the form field |
+
+`error-messages` object can override error messages for validation.
+
+| Attribute | Type | Default |
+| ---- | ---- | ---- |
+| `required` | string | Mandatory. |
+| `email` | string | Invalid email. |
+| `min` | string | Too low (1 min). |
+| `max` | string | Too high (100 max). |
+| `minlength` | string | Too short (6 characters min). |
+| `maxlength` | string | Too high (12 characters max). |
+| `pattern` | string | Invalid format. |
diff --git a/packages/oui-field/src/field.component.js b/packages/oui-field/src/field.component.js
index e3364c88..dea42744 100644
--- a/packages/oui-field/src/field.component.js
+++ b/packages/oui-field/src/field.component.js
@@ -4,7 +4,8 @@ import template from "./field.html";
export default {
bindings: {
label: "@?",
- helpText: "@?"
+ helpText: "@?",
+ errorMessages: ""
},
controller,
require: {
diff --git a/packages/oui-field/src/field.controller.js b/packages/oui-field/src/field.controller.js
index 7f1d3a84..3b2228fa 100644
--- a/packages/oui-field/src/field.controller.js
+++ b/packages/oui-field/src/field.controller.js
@@ -22,7 +22,8 @@ const VALIDATION_PARAMETERS = {
min: ["min", "ng-min"],
max: ["max", "ng-max"],
minlength: ["minlength", "ng-minlength"],
- maxlength: ["maxlength", "ng-maxlength"]
+ maxlength: ["maxlength", "ng-maxlength"],
+ pattern: ["pattern", "ng-pattern"]
};
export default class FieldController {
@@ -124,7 +125,7 @@ export default class FieldController {
showErrors (controlElement, name) {
this.$timeout(() => {
- if (this.form[name].$invalid) {
+ if (this.form[name] && this.form[name].$invalid) {
angular.element(controlElement).addClass(FieldController.getErrorClass(controlElement));
this.$ouiFieldElement.addClass("oui-field_error");
this.isErrorVisible = true;
@@ -149,7 +150,7 @@ export default class FieldController {
getFirstError () {
const names = Object.keys(this.controls);
for (let i = 0; i < names.length; ++i) {
- if (this.form[names[i]].$invalid) {
+ if (this.form[names[i]] && this.form[names[i]].$invalid) {
return this.form[names[i]].$error;
}
}
@@ -157,8 +158,12 @@ export default class FieldController {
return null;
}
+ getMessageString (errorName) {
+ return (this.errorMessages && this.errorMessages[errorName]) || this.ouiFieldConfiguration.translations.errors[errorName];
+ }
+
getErrorMessage (errorName) {
- const message = this.ouiFieldConfiguration.translations.errors[errorName];
+ const message = this.getMessageString(errorName);
const parameterValue = this.validationParameters[this.currentErrorField][errorName];
return message.replace(`{{${errorName}}}`, parameterValue);
}
diff --git a/packages/oui-field/src/field.html b/packages/oui-field/src/field.html
index 8f9ea9cd..7c2bb161 100644
--- a/packages/oui-field/src/field.html
+++ b/packages/oui-field/src/field.html
@@ -22,6 +22,8 @@
ng-bind="$ctrl.getErrorMessage('min')">
+
{
expect(getLabel(element)).toBeNull();
});
- it("should set the for attribute on label", () => {
+ it("should set the 'for' attribute on label", () => {
const id = "lastname";
const element = TestUtils.compileTemplate(`
@@ -90,6 +90,60 @@ describe("ouiField", () => {
expect(getLabel(element).getAttribute("for")).toEqual(id);
});
+ it("should trigger error when invalid format", () => {
+
+ const element = TestUtils.compileTemplate(`
+
+ `);
+ const controller = getField(element).controller("ouiField");
+
+ $timeout.flush();
+
+ const $control = getControl(controller, "username");
+ $control.val("ch@t12");
+ $control.triggerHandler("input");
+ $control.triggerHandler("blur");
+
+ expect(controller.getFirstError().pattern).toBeTruthy();
+ });
+
+ it("should trigger error when length too short", () => {
+
+ const element = TestUtils.compileTemplate(`
+
+ `);
+ const controller = getField(element).controller("ouiField");
+
+ $timeout.flush();
+
+ const $control = getControl(controller, "username");
+ $control.val("abc");
+ $control.triggerHandler("input");
+ $control.triggerHandler("blur");
+
+ expect(controller.getFirstError().minlength).toBeTruthy();
+ });
+
it("should set the name of the form field in the controller", () => {
const name = "lastname";
@@ -128,16 +182,16 @@ describe("ouiField", () => {
id="age"
name="age"
ng-model="$ctrl.user.age"
- min="{{$ctrl.validation.min}}"
- max="{{$ctrl.validation.max}}">
+ ng-min="{{$ctrl.validation.min}}"
+ ng-max="{{$ctrl.validation.max}}">
+ ng-minlength="{{$ctrl.validation.minlength}}"
+ ng-maxlength="{{$ctrl.validation.maxlength}}">
`, {
validation
@@ -466,6 +520,72 @@ describe("ouiField", () => {
});
});
+ describe("with validation", () => {
+ it("should retrieve custom error messages", () => {
+ const message = "Username must be a least 6 characters.";
+
+ const element = TestUtils.compileTemplate(`
+
+ `);
+
+ const controller = getField(element).controller("ouiField");
+
+ $timeout.flush();
+
+ const $control = getControl(controller, "username");
+ $control.val("abc");
+ $control.triggerHandler("input");
+ $control.triggerHandler("blur");
+
+ $timeout.flush();
+
+ expect(controller.getFirstError().minlength).toBeTruthy();
+ expect(controller.getErrorMessage("minlength")).toBe(message);
+ });
+
+ it("should give a message containing parameters", () => {
+ const messageMinlength = 5;
+
+ const element = TestUtils.compileTemplate(`
+
+ `);
+
+ const controller = getField(element).controller("ouiField");
+
+ $timeout.flush();
+
+ const $control = getControl(controller, "username");
+ $control.val("abc");
+ $control.triggerHandler("input");
+ $control.triggerHandler("blur");
+
+ $timeout.flush();
+
+ expect(controller.getFirstError().minlength).toBeTruthy();
+ expect(controller.getErrorMessage("minlength")).toContain(messageMinlength);
+ });
+ });
});
});