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

custom input validator function example contains syntax error and is unclear #6585

Closed
michahell opened this issue Aug 22, 2017 · 5 comments
Closed

Comments

@michahell
Copy link

the following example, here:
https://material.angular.io/components/input/overview
located under Custom Error Matcher contains one too many right brackets
and on top of that, is unclear:

function myErrorStateMatcher(control: FormControl, form: FormGroupDirective | NgForm): boolean {
  // Error when invalid control is dirty, touched, or submitted
  const isSubmitted = form && form.submitted;
  return !!(control.invalid && (control.dirty || control.touched || isSubmitted)));
}

why not just use:

return control.invalid && (control.dirty || control.touched || isSubmitted);

?

@willshowell
Copy link
Contributor

willshowell commented Aug 22, 2017

Good catch on the extra right parentheses.

For the boolean coercion, that example was based on the existing default matcher. It originates from #3560 (comment)

@michahell
Copy link
Author

michahell commented Aug 22, 2017

ty, but why is coercion necessary? does && not already do that ? or is there some javascript wat involved if any of the control.dirty or other properties are not booleans? if so, wat.

@willshowell
Copy link
Contributor

Yeah I think in this case it is not actually necessary (maybe just for safety?). && uses the last value, but as long as dirty, touched, and submitted are all boolean, there should be no need to coerce it.

> 1 && true
true

> true && 1
1

> !!(true && 1)
true

@michahell
Copy link
Author

A case of wat, it was, then :) I unconsciously knew this about &&, but only now realise that coercing might sometimes be necessary, thanks!

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants