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

Asynchronous resolution of validation messages #334

Closed
0cv opened this issue Dec 10, 2016 · 8 comments
Closed

Asynchronous resolution of validation messages #334

0cv opened this issue Dec 10, 2016 · 8 comments

Comments

@0cv
Copy link

0cv commented Dec 10, 2016

**I'm submitting a Feature (or Support) request ** (check one with "x")

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

Current behavior
This is possible to customise validation messages in the forRoot function. However, even so it's possible to tweak the messages a bit by using a synchronous function, this does not seem possible to resolve messages asynchronously, through e.g. Observable (let me know if I missed something).

Expected behavior
Depending on the user language filling the form, it would be great to have an advanced customisation of error messages. Observable would give the possibility to fetch data possibly remotely.

What is the motivation / use case for changing the behavior?
More flexibility

@0cv
Copy link
Author

0cv commented Dec 10, 2016

Guess I found something which fulfils partially what I'm asking: https://github.com/formly-js/ng2-formly/blob/503e803e27896f73042ef3024cad8321adf4a749/demo/hello.ts#L221

         validators: {
            city: {
              expression: (control: FormControl) => control.value && control.value.length > 3,
              message: `City: Should have atleast 3 Characters`,
            },
          },

But it seems to only work when an expression is specified as well. Is there anything else?

@mohammedzamakhan
Copy link
Contributor

There's #307 that's in the pipeline. We hope that someone can help us with it by making their first PR for the repo 😁

@0cv
Copy link
Author

0cv commented Dec 10, 2016

This sounds good, but I'm not sure that's exactly the same. Does Field.level.messages applies at Form or at Field level? In my case, I'm not looking specifically at customising the message at field level, but instead looking at customising generic error messages, at least at Form level or ideally at application level (e.g. the error message for required or minLength). A bit like this https://github.com/formly-js/ng2-formly/blob/master/demo/validation.service.ts#L6-L9

@mohammedzamakhan
Copy link
Contributor

For that you always have setValidator in FormlyConfig https://github.com/formly-js/ng2-formly/blob/master/src/core/services/formly.config.ts#L138

@0cv
Copy link
Author

0cv commented Dec 10, 2016

We are getting closer. So much like this in forRoot, but instead directly in the component https://github.com/formly-js/ng2-formly/blob/master/demo/hello.ts#L552 ? However this does not seem to be the place where the messages can be customized, isn't it? Feels like setValidationMessages or similar is missing on FormlyConfig

@mohammedzamakhan
Copy link
Contributor

Makes sense 👍 I thought there is already a way for that. Can you make a PR for that?

@0cv
Copy link
Author

0cv commented Dec 11, 2016

While trying to make the PR, I realized that actually it already exists... Just need to include the service private validationMessages: FormlyValidationMessages in the Component constructor and then for example in ngOnInit to configure the required message: this.validationMessages.addStringMessage('required', 'Field required!');

It works also when wrapped into a setTimeout so ultimately, that's all what's needed I guess

@0cv 0cv closed this as completed Dec 11, 2016
@rogercoder
Copy link

Using ngx-formly 4.8.0, you now include private formlyConfig: FormlyConfig in your constructor, and then, in ngOnInit():

this.formlyConfig.addValidatorMessage("required", 'This field is required');

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

No branches or pull requests

3 participants