-
Notifications
You must be signed in to change notification settings - Fork 7
feat(field): add custom error message and ng-pattern support #109
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,17 @@ | |
| name="email" | ||
| ng-model="$ctrl.user.email"> | ||
| </oui-field> | ||
|
|
||
| <oui-field label="{{'Username'}}" | ||
| error-messages="{pattern: 'Username must be alphabetic with a length between 3 and 8.'}"> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to see parameters in this example |
||
| <input | ||
| class="oui-input" | ||
| type="text" | ||
| id="username" | ||
| name="username" | ||
| ng-model="$ctrl.user.username" | ||
| ng-pattern="/^[a-zA-Z]{3,8}$/"> | ||
| </oui-field> | ||
| ``` | ||
|
|
||
| ### 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. | | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,7 +69,7 @@ describe("ouiField", () => { | |
| 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(` | ||
| <form name="form"> | ||
| <oui-field label="{{'username'}}"> | ||
| <input type="text" | ||
| class="oui-input" | ||
| type="text" | ||
| id="username" | ||
| name="username" | ||
| ng-model="$ctrl.username" | ||
| ng-pattern="/^[a-zA-Z]{3,8}$/"> | ||
| </oui-field> | ||
| </form> | ||
| `); | ||
| 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(` | ||
| <form name="form"> | ||
| <oui-field label="{{'username'}}"> | ||
| <input type="text" | ||
| class="oui-input" | ||
| type="text" | ||
| id="username" | ||
| name="username" | ||
| minlength="6" | ||
| ng-model="$ctrl.username"> | ||
| </oui-field> | ||
| </form> | ||
| `); | ||
| 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}}"> | ||
| <input | ||
| class="oui-input" | ||
| type="text" | ||
| id="name" | ||
| name="name" | ||
| ng-model="$ctrl.user.name" | ||
| minlength="{{$ctrl.validation.minlength}}" | ||
| maxlength="{{$ctrl.validation.maxlength}}"> | ||
| ng-minlength="{{$ctrl.validation.minlength}}" | ||
| ng-maxlength="{{$ctrl.validation.maxlength}}"> | ||
| </oui-field> | ||
| `, { | ||
| validation | ||
|
|
@@ -466,6 +520,72 @@ describe("ouiField", () => { | |
| }); | ||
| }); | ||
|
|
||
| describe("with validation", () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good test for custom message!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but it the test should not enforce a string but verify it's actually using the one from the provider
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I agree, provider should be mocked and we should only "verify it's actually using the one from the provider" |
||
| it("should retrieve custom error messages", () => { | ||
| const message = "Username must be a least 6 characters."; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (optional but should not be) I don't remember if it has been done before but I think you should also test that interpolation works in custom messages.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be done in a new test, only for testing the interpolation. |
||
|
|
||
| const element = TestUtils.compileTemplate(` | ||
| <form name="form"> | ||
| <oui-field label="{{'username'}}" | ||
| error-messages="{minlength: '${message}'}"> | ||
| <input type="text" | ||
| class="oui-input" | ||
| type="text" | ||
| id="username" | ||
| name="username" | ||
| ng-minlength="6" | ||
| ng-model="$ctrl.username"> | ||
| </oui-field> | ||
| </form> | ||
| `); | ||
|
|
||
| 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(` | ||
| <form name="form"> | ||
| <oui-field label="{{'username'}}"> | ||
| <input type="text" | ||
| class="oui-input" | ||
| type="text" | ||
| id="username" | ||
| name="username" | ||
| ng-minlength="${messageMinlength}" | ||
| ng-model="$ctrl.username"> | ||
| </oui-field> | ||
| </form> | ||
| `); | ||
|
|
||
| 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); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add a helper text "Username must be alphabetic with a length between 3 and 8.", it will be helpful to trigger error. It could be interesting for other examples too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An helper text or an error, not both of them...
Maybe something like "This input is valid only if..." for helper text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would avoid also doing both, I think the error is easy to trigger like the lastname field.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
easy to trigger yes!
But the user doesn't know the validations without clicking on "Show the example" and reading the code.
"I think the error is easy to trigger like the lastname field." There is already a help text for lastname "At least 3 chars" and it's a good thing that we should reproduce