Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Implemented custom validation in the Angular control #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Implemented custom validation in the Angular control #15

wants to merge 4 commits into from

Conversation

ploutarchos
Copy link
Contributor

The combo-box will now validate whether the selected or typed value is a valid value (i.e. it matches to one of the items of the selection list). If it does, the control marks itself as valid, otherwise it becomes invalid.

A new input property has been introduced, the inputErrorClass to allow us format the input box when the control is marked as invalid.

This implementation works with both ngModel forms and Reactive Forms.

Note: The validation does not occur when remote data is being used. In that case, the control is always marked as invalid which is backwards compatible.

@shochdoerfer
Copy link
Member

Thanks for your contribution. We'll check the PR as soon as we can.

It is not needed since styling of invalid status can now be achieved from
the ng-invalid class that angular adds to the wrapper html element when
the component becomes invalid.
Always initialise the list data to an empty array to fix a console error.
Changed combo box to enforce a validation check on start up after the list data has been populated from the host component. This will always make the control invalid on start up, but at the same time it will be pristine and untouched.
Changed combo box to generate two different validation errors "empty_input" and "input_no_match".
@shochdoerfer shochdoerfer added this to the 0.6.0 milestone May 13, 2017
@shochdoerfer
Copy link
Member

We tested the PR with one of our sample projects but got an error. We need investigate a bit more what when wrong. Once we know, I'll let you know.

@shochdoerfer shochdoerfer modified the milestones: 0.6.0, 0.7.0 Jun 3, 2017
@ploutarchos
Copy link
Contributor Author

ploutarchos commented Jun 11, 2017

What was the error you got?

Also, what are the input properties you used in your app-combo-box?

I tried it with the following ones, and I didn't get any error:

In the Angular template (*.html) add this:

<app-combo-box [listData]="list" [displayField]="'name'" [valueField]="'name'"
                              [inputClass]="'input-class'"
                              [typeAheadDelay]="10"
                              [localFilter]="true"
                              [forceSelection]="false"
                              [(ngModel)]="selectedValue" name="combo"
                              #formControl="ngModel"
></app-combo-box>

Has Error 'empty_input': {{formControl.hasError('empty_input')}}
<br>
Empty input error message: {{formControl.errors.empty_input}}
<br>
Has Error 'input_no_match': {{formControl.hasError('input_no_match')}}
<br>
Input no match error message: {{formControl.errors.input_no_match}}

In the Angular Component (*.ts) add this:

selectedValue;

list: Object[] = [
	{name: 'Test 1'},
	{name: 'Test 2'},
	{name: 'Test 3'},
	{name: 'Test 4'},
	{name: 'Test 5'},
	{name: 'Test 6'},
	{name: 'Test 7'},
	{name: 'Test 8'}
  ];

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants