-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add support for form async validators + unique custom validator #15
Conversation
Validators are relative to forms, not to model or services and should be standalone. They should be in their specific class instead of NaturalAbstractModelService. Can you refactor into an NaturalValidators.unique(args). The count function may be out of the class context but in the same file without export mention.
// Not exported variable, not in class neither. It's like a private function for current file.
const count = (queryVariablesManager, name) => {
...
}
export class NaturalValidators {
public static unique(fieldName: string, service: AbstractModelService<any, any, ...> , apollo: Apollo, name: string) {
...
count(...);
...
}
} Usage would be more standard public getFormAsyncValidators(): FormAsyncValidators {
return {
code: [NaturalValidators.unique('code', this.userService, this.apollo, 'user')],
numberField : [Validators.min(10)] // angular native validator usage for comparison
};
} |
disabled: disabled, | ||
}; | ||
|
||
config[key] = new NaturalFormControl(formState, null, asyncValidators[key]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure about that "null". Don't change that for now, I'll test it by myself, and implement tests.
The rest seems ok, good job. When the refactor is done, we can merge. We have to keep in mind that a debounce is still to do (in another commit/PR) |
filter: {groups: [{conditions: [condition]}]}, | ||
}; | ||
qvm.set('variables', variables); | ||
this.count(qvm).pipe( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldnt this be returned ?
Thanks for the feedback. I've relocated the validator in its own class |
Async Form Validators are useful to validate a form field value using a backend API.
This adds
getFormAsyncValidators()
similar togetFormValidators()
, called bygetFormConfig()
.Is also adds the generic
uniqueValidator()
method returning anAsyncValidatorFn
that can be used to check any model and field for existing values.Usage in model (e.g
Product.code
):