Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors should display useful informations. #12

Closed
gpessa opened this issue Aug 8, 2018 · 8 comments
Closed

Errors should display useful informations. #12

gpessa opened this issue Aug 8, 2018 · 8 comments
Assignees

Comments

@gpessa
Copy link

gpessa commented Aug 8, 2018

IMPORTANT:
Validator like "greater than" (equalTo has the same issue) should return back the value which is suppose to be bigger of so we can include the information in the error we have to display.

Steps to reproduce and a minimal demo
Add a validator to the field and check the value of the error.

<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [gt]="10">
<p *ngIf="field.errors?.gt">error message</p>

Current behavior
The error return a simple boolean.

Expected/desired behavior

{
  greaterThan: 3
}

in this way i can do something like:

<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [gt]="10">
<p *ngIf="field.errors?.gt">field should be greater than {{ field.error.gt }}</p>
@rsaenen
Copy link
Owner

rsaenen commented Aug 8, 2018

Okay, it will be released soon

@rsaenen rsaenen self-assigned this Aug 8, 2018
@gpessa
Copy link
Author

gpessa commented Aug 9, 2018

Love u!
Would you like to have a list of validator that could require a bit of this refactoring?

@rsaenen
Copy link
Owner

rsaenen commented Aug 14, 2018

This take a bit more time as I have to rewrite a lot of unit test, sorry

@gpessa
Copy link
Author

gpessa commented Aug 14, 2018

You are doing an amazing job. I love the validators.
Take all the time you need to keep the code quality high.

Thanks again!

@rsaenen
Copy link
Owner

rsaenen commented Aug 19, 2018

Thanks! Released with 6.0.3.

The paramater of each validator (if it has) can be accessible in the template with reason.

<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [gt]="10">
<!--> Will display : error message and must be greater than 10 <-->
<p *ngIf="field.errors?.gt">error message and must be greater than {{ field.errors?.reason }}</p>1

@gpessa
Copy link
Author

gpessa commented Aug 21, 2018

Dow :(
Is different from the way the standard angular validators work.
Your way override the reasons if more than one validator is there.
This is how it looks:

Actual situation:

controls.errors : { required: true, gt: true, reason: 10 }

Desired situation:

controls.errors : { required: true, gt: { reason: 10 } }

in this way you can have multiple validators which do not override each-others reasons

@rsaenen
Copy link
Owner

rsaenen commented Aug 21, 2018

Following the Material Guideline, you shouldn't display more than one error message.

In your example, gt won't trigger an error when the field has already a required error.

Nevertheless, put the reason in gt is (technically) a better choice.
Can you make a PR?

@rsaenen
Copy link
Owner

rsaenen commented Sep 25, 2018

Released with 6.10

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

No branches or pull requests

2 participants