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

validateAfterLeaveOrPressedEnter? #309

Closed
ericpyle opened this issue Oct 9, 2017 · 17 comments
Closed

validateAfterLeaveOrPressedEnter? #309

ericpyle opened this issue Oct 9, 2017 · 17 comments

Comments

@ericpyle
Copy link

ericpyle commented Oct 9, 2017

I'm a little confused by the current field validation options.

There is validateAfterLoad, and validateAfterChanged (as user types).

How can I validate a field only after a user leaves the control or presses enter (which I believe is the typical way that field validators work?) I don't really want to bother the user that what they are typing is invalid until they are finished typing it and ready to move on.

Thanks for any help!

@ericpyle ericpyle changed the title validateAfterLeaveOrPressedEnter validateAfterLeaveOrPressedEnter? Oct 9, 2017
@ericpyle
Copy link
Author

ericpyle commented Oct 9, 2017

OR if I asynchronously validate a text box as the user types (after each change), how do I cancel all but the last validation when the user stops typing?

I want to avoid getting multiple errors as the user types (just care about the final one--which may come back earlier than others)
image

@ericpyle
Copy link
Author

Perhaps there is a way for vfg to integrate https://lodash.com/docs#debounce for text inputs ?

@icebob
Copy link
Member

icebob commented Oct 10, 2017

You should integrate debounce to your async validator.

@ericpyle
Copy link
Author

@icebob thanks for that suggestion. The examples I had seen with debouncer were trying to control data, but using it to just control the validation should work.

@ericpyle
Copy link
Author

@icebob having some success, but not quite with _debounce(). Now the error message does not get reported until after I start typing a second time. Is the _debounce() promise not quite what you expected or am I doing something not right?

validator: _.debounce(async function(value, field, model){
                            try {
                                var response = await remote.get(`/validate/${value}`);
                                console.log(response);
                            } catch (error) {
                                console.log(error);
                                return [error.responseJSON.error_message];
                            } finally{
                                this.$root.validating = false;
                            }
                            return [];
                        }, 500)

After typing 123456, I hit a breakpoint in my debounce callback, and it returns [error_message]
image

However, I don't see the error message until I start typing again (e.g. "22")

image

@ericpyle ericpyle reopened this Oct 10, 2017
@ericpyle
Copy link
Author

ericpyle commented Oct 10, 2017

Not sure it matters, but the first time I type 123456, my onValidated() gets called 6 times with isValid set to true.

methods: {
            onValidated(isValid, errors) {
                console.log("Validation result: ", isValid, ", Errors:", errors);
            },

image

Then the next time I type "2", I get two onValidated calls, the first one isValid is true, the last one is false.

image

@icebob
Copy link
Member

icebob commented Oct 10, 2017

Would be good a jsFiddle with this issue in order to we can investigate it.

@ericpyle
Copy link
Author

ericpyle commented Oct 10, 2017

@icebob here's a jsFiddle to reproduce the issue
https://jsfiddle.net/ericpyle/rnsgn2ng/

@icebob
Copy link
Member

icebob commented Oct 10, 2017

Great, thanks!

@ericpyle
Copy link
Author

Thanks, @icebob

Also wanted to report that sometimes I still get multiple errors:
image

@Alex-Sokolov
Copy link

Alex-Sokolov commented Oct 11, 2017

That's really needed. Now typing every char will start validation and UI freeze for some time if typing word fast

@icebob
Copy link
Member

icebob commented Oct 11, 2017

Hi @ericpyle,
I tested and it seems vfg validation can't work properly with lodash debounce. :(

@icebob
Copy link
Member

icebob commented Oct 11, 2017

I started to add debouncing to vfg in validation process. I think it will solve your problem.
Fiddle

@ericpyle
Copy link
Author

@icebob yes that seems to work just fine and I no longer see multiple errors being reported. how do you recommend I get a version of vfg with the fix?

@icebob
Copy link
Member

icebob commented Oct 11, 2017

This week I will make a new release.

@ericpyle
Copy link
Author

@icebob thanks

@icebob icebob closed this as completed in 20bbd59 Oct 12, 2017
@icebob
Copy link
Member

icebob commented Oct 20, 2017

Released in v2.1.1

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

No branches or pull requests

3 participants