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

validate onBlur #91

Merged
merged 3 commits into from
May 31, 2019
Merged

validate onBlur #91

merged 3 commits into from
May 31, 2019

Conversation

ej2015
Copy link
Contributor

@ej2015 ej2015 commented May 29, 2019

Ref #42. My attempt to enable individual inputs validation on blur.

It adds #showMessage, which is called by onBlur. To make this work, I changed the message method to always create the error element, and toggle its display properties as needed. Internally each element is created with an id ${field}-validator. #showMessage will look find the element by id and toggle its visibility.

In a typical React component, it will look something like this:

...
  handleBlur = field => (
    () => this.validator.showMessage(field)
  )
...
  <input 
     value={this.state.name} 
     onChange={this.setName}
     onBlur={this.handleBlur('name')}
  />
{validator.message('name', this.state.name, 'required|email')}
...

So far it's only working for react app. Will be happy to hear others' opinions with this approach and how to handle all the use cases here

if (options.element === false) {

@stuyam
Copy link
Collaborator

stuyam commented May 30, 2019

Hmm yeah it's a little weird to set the display type, and use an id to find it, not very reacty and won't work for react native. I think all we need is an internal reference for each option and whether we should show messages on it yet or not.

I appreciate the contribution though this is a good first stab at making this happen! Thank you 🙏

@stuyam stuyam self-assigned this May 30, 2019
@ej2015
Copy link
Contributor Author

ej2015 commented May 30, 2019

I made some changes to set internal flags through onBlur events. Yes this approach is much better. Now the usage would look like this:

...
  handleBlur = field => (
    () => {
      this.validator.showMessage(field)
      this.forceUpdate()
  })
...
  <input 
     value={this.state.name} 
     onChange={this.setName}
     onBlur={this.handleBlur('name')}
  />
{validator.message('name', this.state.name, 'required|email')}

I think it can handle all cases now, as the toggle is back where it was

if (this.messagesShown || this.visibleFields.includes(field)) {

@stuyam
Copy link
Collaborator

stuyam commented May 31, 2019

I like this approach a lot better! Nice work, I'm gonna merge this and add a method to remove a shown message.

@stuyam stuyam merged commit f3c227e into dockwa:master May 31, 2019
@stuyam stuyam mentioned this pull request May 31, 2019
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 this pull request may close these issues.

2 participants