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
NumericValidator - differentiate errors for allowDecimal true/false #311
Comments
@ChrisKaun Thanks for sharing the detailed information. The mentioned concern can be managed through height: [
'',
RxwebValidators.numeric({
acceptValue: NumericValueType.PositiveNumber,
allowDecimal: true,
messageKey: 'mustBeADecimal'
})
],
age: [
'',
RxwebValidators.numeric({
acceptValue: NumericValueType.PositiveNumber,
allowDecimal: false,
messageKey: 'mustBeAnInteger'
})
] As per the above code I replace the |
@ajayojha That sould work in my use case. The only thing that I would suggest is to mention that option in the docs. |
@ajayojha I tried the numeric in the model using the decorator, but the format function is not working. Here is my model definition
Do you know any reason why it does not work? I also tried with RxwebValidators.numeric({}) to define form but it also does not work. I am using the latest angular version |
@hoang-innomizetech Please share the stackblitz example. |
@ajayojha I just fixed this issue by importing the RxReactiveFormsModule into my app. Previously, I only wanted to configure the RxFormBuilder provider to avoid import that module |
Thanks for the update, I thought you have imported the module and still you are facing the issue. Anyways it’s good to know that numeric format is working as expected.
Get Outlook for iOS<https://aka.ms/o0ukef>
…________________________________
From: Hoang Le <notifications@github.com>
Sent: Tuesday, April 21, 2020 3:11:36 PM
To: rxweb/rxweb <rxweb@noreply.github.com>
Cc: Ajay Ojha <ojhaajay@outlook.com>; Mention <mention@noreply.github.com>
Subject: Re: [rxweb/rxweb] NumericValidator - differentiate errors for allowDecimal true/false (#311)
@ajayojha<https://github.com/ajayojha> I just fixed this issue by importing the RxReactiveFormsModule into my app. Previously, I only wanted to configure the RxFormBuilder provider to avoid import that module
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#311 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AE3SS3UZEQUHLGN4H46CAKLRNVS5BANCNFSM4MMPEOSQ>.
|
@ajayojha Just tested the |
@ChrisKaun Please refer to the stackblitz example. It's working as expected. The key property won't change but the error message will be changed based upon the provided Testing Steps:
Please highlight if something is incorrect. |
Okay this works, didn't know that I need to register the validation messages. This won't work for my usecase since I need to change the error key, not the message itself. The key is used in a hash to lookup the translation for the current locale. |
Which translation package you are using?
can you share code snippet?
Please comment the above question so that I can help you out accordingly
Get Outlook for iOS<https://aka.ms/o0ukef>
…________________________________
From: ChrisKaun <notifications@github.com>
Sent: Tuesday, April 21, 2020 8:08:49 PM
To: rxweb/rxweb <rxweb@noreply.github.com>
Cc: Ajay Ojha <ojhaajay@outlook.com>; Mention <mention@noreply.github.com>
Subject: Re: [rxweb/rxweb] NumericValidator - differentiate errors for allowDecimal true/false (#311)
Okay this works, didn't know that I need to register the validation messages.
This won't work for my usecase since I need to change the error key, not the message itself. The key is used in a hash to lookup the translation for the current locale.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#311 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AE3SS3WPC772ZXTXFDLR2C3RNWVXTANCNFSM4MMPEOSQ>.
|
In this case none. I simply use the key in a hash object to lookup the translation.
Just a basic fork of your stackblitz. Added a weight control that uses a custom numeric validator. The error message key is added dependent on the used method. Then I can use the key for translation lookup. Same logic would work with your messageKey option, but then I would need to translate all messages, instead of the keys. |
@ChrisKaun I try to accommodate multilingual error messages as per your case with the lib. Please refer to the stackblitz example: https://stackblitz.com/edit/rxweb-numeric-message-key-example?file=src%2Fapp%2Fapp.component.ts |
The numeric format is working but it doesn't solve my problem. Since our application needs to support multilingual, so we have to find a way to specific locale when formatting instead of using default LOCALE_ID. I read the code and cannot find out any way to do that. I have created this stackblitz and registered some locales, but it seems does not work non-default locale (i.e. de) Do you have any ideas to make this works? |
@hoang-innomizetech I will check and let you know in two hours, As I am in between something to releasing translation package. |
@ajayojha We are using ngx-translate library for translation. We need the messageKey value passed to the error object along with the message prop so that we don't have to map the message and key again. Is this possible? |
@hoang-innomizetech I am implementing the feature for multilingual messaging with our translation package and that works with other translation packages as well, I hope so :). It would be great if you share a small code snippet of form control error message with ngx-translate. Because this is helpful for me to cover your project case as well, If I missed something. |
@hoang-innomizetech I reviewed the provided example, As such, there is no feature available that works with multiple locales. I am thinking to resolve the decimal separator and group separator on demand, which can be configured on What do you think, will it be helpful for your project? Feel free to share your inputs :). |
I read the code and noticed that we can configure the number globally using
And if we configure the ReactiveFormConfig on demand, we might have an issue with the formatted value, it will not reformat without refresh the page. |
I am closing this ticket as no further action required on this :). |
Describe the bug
Using a NumericValidator on two controls is an issue if one allows decimals and one forbids them, since the NumericValidator will set the error
numeric
to false if the value is invalid.Assuming we create a FormGroup with a
height
and aage
control:And we translate the
numeric
error message asMust be a valid number
this would make sense if the user enters any non-numeric values to the input.But as soon as the user enters a decimal value to the
age
the control will get a translated error messageMust be a valid number
displayed.Translating the error message to
Can't be a decimal
doesn't make sense since as soon as the user enters non-numeric values any of those controls it wouldn't match the input.Expected Behavior
Differentiate the
numeric
error to a decimal and non decimal error.If
allowDecimal
is true, change the key tonumericDecimal
, else leave it asnumeric
or rename it tonumericInteger
.Other option would be to allow a custom message per Validation. There is a
message
parameter but it doesn't seem to have any impact on the error message.Package Version
@rxweb/reactive-form-validators@1.9.8
Browser
Firefox 75.0
The text was updated successfully, but these errors were encountered: