ng-dynamic-forms
ng-dynamic-forms copied to clipboard
Unable to set custom validator property
I'm submitting a
[ ] Bug / Regression
[ ] Feature Request / Proposal
[ X] Question
I'm using
NG Dynamic Forms Version: `6.0.6`
[ ] Basic UI
[ ] Bootstrap UI
[ ] Foundation UI
[ ] Ionic UI
[ ] Kendo UI
[ X] Material
[ ] NG Bootstrap
[ ] Prime NG
Description
I have an issue with trying to set a customValidator
. Whenever I use the the provided function in app.validators.ts I am setting the hasError
depending on if the username is starting with "abc". It does break there and set the hasError
but it is not carried over to setting my validator usernameStartsWithABC: true
. In fact no matter what I do, I am unable to change the value from null to anything else. What am I doing wrong or is this a bug?
In Model:
Browser showing no change to custom validator usernameStartsWithABC
:
Validator function:
If we use custom validators in a group (not directly tied to a field), is it possible to generate the final message (from the model errorMessages
template) and return the corresponding message from the validator ? In this example, we can see that the message returned is hardcoded to true
. I know that under the hood, the DynamicFormValidationService
has a function to parseErrorMessageConfig
, but is it possible to use it for those validations that are not tied to a specific field ? It seems like the only parameter for the custom validator is a FormGroup (in the example you gave in the readme you are clearly using the validator with a control because the parameter is an AbstractControl
). I'm just wondering if it's a bug or a feature that has not been implemented yet where using a custom validator for a group will not process the corresponding error message ?
Thanks !
@rstor Hi!
There is a misconception here!
The validators
object does not reflect the current error state of a form control.
It just statically references the validator functions that should be applied to the form control.
null
here means that the validator function can be used directly and does not have to be called as factory function.
@dcorriveau-omniscient
I don't think I'm quite sure what you're asking for.
You can apply errorMessages
to a DynamicFormGroupModel
as well.
This is shown, for instance, in Material sample.
@udos86 thanks for the reply! To be honest, how these validators work in the model is still unclear, but getting there. I do have some related questions though:
When I retrieve the errorMessages
from the model as such:
the interpolation {{ placeholder }}
is not showing the placeholder value I have set in the model. Why is this?
Lastly, would you consider a more comprehensive doc on how to properly use the validators along with custom components?
@udos86 My question was related to the use of validators with DynamicFormGroupModel AND CustomValidators AND error messages with replacement tokens (all at the same time) AND Custom Dynamic Controls.
The validators object does not reflect the current error state of a form control. It just statically references the validator functions that should be applied to the form control. null here means that the validator function can be used directly and does not have to be called as factory function.
I don't think that's true for CustomValidators though ? Because when I set it to return a specific string, that's what I see in the validators
object (null is replaced with the return value of the function once the validations are fired).
Maybe the issue is related to the use of Custom Dynamic Controls and my implementation is wrong ... ?
Thank you !
@udos86, I am implementing custom validator. Validator is getting executed but not showing error message. Could you please give some detailed documentation that how we have to show error message in case of custom validations.
@rstor, If you were able to achieve this, could you please let me know what I am doing wrong?