question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

validate on blur for class component

See original GitHub issue

I’m writing my React components using the new ES6 class syntax and the Formsy.HOC method. I’ve tried implementing validate on blur, but have been unable to get it to work.

Whenever I try typing into the input, no values get set. Here is my code:

import React, { Component } from 'react';
import { HOC } from 'formsy-react';

class FormInput extends Component {
  changeValue(event) {
    if (this.props.getErrorMessage() != null) {
      this.props.setValue(event.currentTarget.value);
    } else {
      if (this.props.isValidValue(event.target.value)) {
        this.props.setValue(event.target.value);
      } else {
        this.setState({
          _value: event.currentTarget.value,
          _isPristine: false
        });
      }
    }
  }

  blurValue(event) {
    this.props.setValue(event.currentTarget.value);
  }

  render() {
    return (
      <div>
        <input type="text" onBlur={this.blurValue.bind(this)} onChange={this.changeValue.bind(this)} value={this.props.getValue() || ''} />
    );
  }
};

export default HOC(FormInput);

I’m sure the example on the wiki works using mixins, but how can I get it to work with the setup I have above?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
aesopwolfcommented, Dec 15, 2016

Give this a try:

import React, { Component } from 'react';
import { HOC } from 'formsy-react';

class FormInput extends Component {
  constructor(props) {
    super(props);

    this.state = {
      blurred: false,
    };
  }

  handleBlur() {
    this.setState({
      blurred: true,
    })
  }

  handleFocus() {
    this.setState({
      blurred: false,
    })
  }

  render() {
    var errorMessageFromBluring = this.state.blurred ? this.props.getErrorMessage() : '';
    return (
      <div>
        <p>{errorMessageFromBluring}</p>
        <input
          type="text"
          onFocus={this.handleFocus.bind(this)}
          onBlur={this.handleBlur.bind(this)}
          onChange={e => this.props.setValue(e.target.value)}
          value={this.props.getValue() || ''}
        />
      </div>
    );
  }
}

export default HOC(FormInput);
0reactions
Extra-lightwillcommented, Dec 16, 2016

@aesopwolf Really cool, this works. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS - using onBlur to validate email field - Stack Overflow
I think the right way to do so is by using internal state of the component: class EmailComponent extends React.
Read more >
onBlur validation with react-material-ui-form-validator · GitHub
onBlur validation with react-material-ui-form-validator - Component.jsx. ... export default class SomeComponent extends React.Component {. constructor() {.
Read more >
Client-side Checking with React.js JSX Events Using onBlur
Capturing and applying logic to an HTML onBlur event allows other UI interactivity. This guide will hook in logic with an onBlur event...
Read more >
How to change validation behavior on blur event of lightning ...
I want to validate only at submitting form data via REST API. How can I achieve this? Sample template is simple as below....
Read more >
onblur Event - W3Schools
The onblur event is often used with form validation (when the user leaves a form field). Focus Based Events. Event, Description, Bubbles. focus,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found