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

Pass parameters to validation #1828

Closed
Devqon opened this issue Oct 11, 2019 · 5 comments
Closed

Pass parameters to validation #1828

Devqon opened this issue Oct 11, 2019 · 5 comments

Comments

@Devqon
Copy link
Contributor

Devqon commented Oct 11, 2019

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[x] support request

Current behavior
There is not a pre-defined way to pass in parameters to the validators, other then using templateOptions or some other field. I tried a custom property on the validators object, but it is interpreted as validator on itself.

Expected behavior
There should be a way to pass parameters to a validator in a logical way.

Minimal reproduction of the problem with instructions
https://stackblitz.com/edit/angular-ngx-formly-cck62q

What is the motivation / use case for changing the behavior?
I want to do validation on dates for future and/or past validation.

Please tell us about your environment:
..

  • Angular version: 2.0.X
    7.x

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
    all

  • Language: [all | TypeScript X.X | ES6/7 | ES5]
    Typescript

@MantasJur
Copy link

MantasJur commented Jan 16, 2020

I don't know whether this is a solution to your problem, but when I need extra parameters in my validators, I just make the validator itself a higher order function, taking in some parameters. For example, if I need access to the entire model in my validator I do:

export function SomeValidator(model: any) {
  return (control: AbstractControl) => {
    // do validation
  };
}

And for usage, it's just

validation: [SomeValidator(this.model)]

@Et3rnal
Copy link

Et3rnal commented Jan 21, 2020

@iMantasas I'm trying your approach but all I get is
There is no validator by the name of "dateMustBeAfterValidator(this.model)"
What did you have in FormlyModule.validators?
Could you please provide a Stackblitz ?

@scottux
Copy link

scottux commented Mar 23, 2020

// Wrap your validator with an outer function to pass in the options parameter.
function FutureDateWithOptions(options) {
  return FormlyDateFutureValidator;

  function FormlyDateFutureValidator(c: AbstractControl, field: FormlyFieldConfig): ValidationErrors | null {
    if (!c.value) {
      return null;
    }
    // Use the options parameter if provided, or make a default one.
    options = options || { days: 2 };
// ...

Calling the function with the options object will return a FormlyDateFutureValidator using those options.

validators: [
    { name: 'date-future', validation: FutureDateWithOptions({days: 3}) },
 ]

This is not the cleanest way to do it, as you may want to do a merge of the options.

let defaults = { days: 2 };
// Use the options parameter if provided, or make a default one.
// Merge options with defaults instead of overwriting.
options = Object.assign({}, defaults, options}; 

@aitboudad
Copy link
Member

aitboudad commented Jun 3, 2020

This issue has been fixed and released as part of v5.7 release. Check our demo example https://formly.dev/examples/validation/custom-validation-parameters

Please let us know, in case you are still encountering a similar issue/problem.
Thank you!

@HamzaMoiyadi

This comment has been minimized.

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

No branches or pull requests

6 participants