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

Initial isFieldValid returns true when field is empty and uses required validator. #108

Closed
RyanONeill1970 opened this issue Apr 18, 2024 · 5 comments · Fixed by #110
Closed

Comments

@RyanONeill1970
Copy link
Contributor

On calling v.isFieldValid and passing an element it appears to use the cached version even though it has not previously run.

From the code for isFieldValid I can see the validation is set up to run on the debounce timeout, which I have set to 0 on bootstrap.

This results in isFieldValid returning true even though it has not called the validator. The validator fires after the function returns and evaluates to false. There is no async code.

I'm going to work around this by using the callback.

Is it an error that the state is returned as true when it has not been through a validator? From the docs on this function it's not obvious that the return state is cached.

Would you consider an alternative (which I'll help with) if the debounce is 0, calling the validator and returning the state immediately?
That might still make the return value unreliable if debounce is not 0 though.

Basic idea of what I am doing is below;

<input type="text" data-val="true" data-val-required="Please enter."id="ElementX" name="ElementX" value="" />

// Bootstrap was set with v.debounce = 0;

// On a button click, check if empty field is valid.
let isValid = v.isFieldValid(element));

// Returns true

Thank you

Ryan

@dahlbyk
Copy link
Collaborator

dahlbyk commented Apr 22, 2024

You're absolutely correct that the debounce is making an otherwise synchronous validation async so it immediately returns true. We should fix that.

It's worth mentioning here that returning the cached value is what happens "by design" if there's a remote or other Promise-returning validator involved. Ideally the method would return a Promise<bool>, but that's a breaking change. Maybe v2?

That might still make the return value unreliable if debounce is not 0 though.

This is also correct, and we should fix that.

  1. validateField() should always call the underlying validator, undebounced.
  2. Only the change/input handler should be debounced.

I actually have a branch in progress (for #100) that's adjusting how the callback is wired/unwired, so I can include this fix in that change.

@RyanONeill1970
Copy link
Contributor Author

Well, thank you very much. Sorry to pile stuff on to your plate.

@dahlbyk
Copy link
Collaborator

dahlbyk commented Apr 26, 2024

Give this a try? #110

@RyanONeill1970
Copy link
Contributor Author

Not sure if Phil has made his changes from review. Let me know when the review is good to test.

@dahlbyk
Copy link
Collaborator

dahlbyk commented May 6, 2024

I fixed the bug Phil found. 👍 Please test!

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

Successfully merging a pull request may close this issue.

2 participants