-
Notifications
You must be signed in to change notification settings - Fork 556
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
Comments
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:
And for usage, it's just
|
@iMantasas I'm trying your approach but all I get is |
// 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}; |
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. |
I'm submitting a ... (check one with "x")
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
The text was updated successfully, but these errors were encountered: