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

The Formsy didn't show error message when the input left blank #258

Closed
hungbang opened this issue Oct 11, 2019 · 3 comments
Closed

The Formsy didn't show error message when the input left blank #258

hungbang opened this issue Oct 11, 2019 · 3 comments

Comments

@hungbang
Copy link

The Formsy didn't show error message when the input left blank, even I did set the required for the input

@hungbang
Copy link
Author

Who anyone can help this issue? Thank you in advance !

@hungbang
Copy link
Author

hungbang commented Oct 23, 2019

import React, { Component } from 'react';
import { TextField } from '@material-ui/core';
import { withFormsy } from 'formsy-react';
import _ from '@lodash';
import keycode from 'keycode';

class TextFieldFormsy extends Component {
  changeValue = event => {
    this.props.setValue(event.currentTarget.value);
    if (this.props.onChange) {
      this.props.onChange(event);
    }
  };

  blurValue = event => {
    this.props.setValue(event.currentTarget.value);
    if (this.props.onBlur) {
      this.props.onBlur(event);
    }
  };

  keyDown = event => {
    if (keycode(event) === 'enter') this.setValue(event.currentTarget.value);
    if (this.props.onKeyDown)
      this.props.onKeyDown(event, event.currentTarget.value);
  };

  render() {
    const importedProps = _.pick(this.props, [
      'autoComplete',
      'autoFocus',
      'children',
      'className',
      'defaultValue',
      'disabled',
      'FormHelperTextProps',
      'fullWidth',
      'id',
      'InputLabelProps',
      'inputProps',
      'InputProps',
      'inputRef',
      'label',
      'multiline',
      'name',
      'onBlur',
      'onChange',
      'onFocus',
      'placeholder',
      'required',
      'rows',
      'rowsMax',
      'select',
      'SelectProps',
      'type',
      'variant',
      'requiredError'
    ]);
    const {
      isRequired,
      isPristine,
      isValid,
      showRequired,
      validationErrors
    } = this.props;

    const isRequiredError =
      isRequired() &&
      !isPristine() &&
      !isValid() &&
      showRequired() &&
      validationErrors.requiredError;

    const errorMessage = this.props.getErrorMessage() || isRequiredError;
    const value = this.props.getValue() || '';

    return (
      <TextField
        {...importedProps}
        onChange={this.changeValue}
        onBlur={this.blurValue}
        onKeyDown={this.keyDown}
        value={value}
        error={Boolean(errorMessage)}
        helperText={errorMessage}
      />
    );
  }
}

export default withFormsy(TextFieldFormsy);

@th3fallen
Copy link

This is actually occuring for us too on v2. seems like an oversight to supress the error message when required is set to true.

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

No branches or pull requests

2 participants