How to properly validate input values with React.JS?

前端 未结 9 1059
孤城傲影
孤城傲影 2020-12-22 19:07

I have a simple form. All of the components and state are held in the Page component. There are 2 display headers and 3 input fields. The first input is supposed to be text,

9条回答
  •  有刺的猬
    2020-12-22 19:54

    Your jsfiddle does not work anymore. I've fixed it: http://jsfiddle.net/tkrotoff/bgC6E/40/ using React 16 and ES6 classes.

    class Adaptive_Input extends React.Component {
      handle_change(e) {
        var new_text = e.currentTarget.value;
        this.props.on_Input_Change(new_text);
      }
    
      render() {
        return (
          
    ); } } class Form extends React.Component { render() { return (
    ); } } class Page extends React.Component { constructor(props) { super(props); this.state = { Name: 'No Name', Value_1: '0', Value_2: '0', Display_Value: '0' }; } handle_text_input(new_text) { this.setState({ Name: new_text }); } handle_value_1_input(new_value) { new_value = parseInt(new_value); var updated_display = new_value + parseInt(this.state.Value_2); updated_display = updated_display.toString(); this.setState({ Value_1: new_value, Display_Value: updated_display }); } handle_value_2_input(new_value) { new_value = parseInt(new_value); var updated_display = parseInt(this.state.Value_1) + new_value; updated_display = updated_display.toString(); this.setState({ Value_2: new_value, Display_Value: updated_display }); } render() { return(

    {this.state.Name}

    Value 1 + Value 2 = {this.state.Display_Value}

    ); } } ReactDOM.render(, document.getElementById('app'));

    And now the same code hacked with form validation thanks to this library: https://github.com/tkrotoff/react-form-with-constraints => http://jsfiddle.net/tkrotoff/k4qa4heg/

    const { FormWithConstraints, FieldFeedbacks, FieldFeedback } = ReactFormWithConstraints;
    
    class Adaptive_Input extends React.Component {
      static contextTypes = {
        form: PropTypes.object.isRequired
      };
    
      constructor(props) {
        super(props);
    
        this.state = {
          field: undefined
        };
    
        this.fieldWillValidate = this.fieldWillValidate.bind(this);
        this.fieldDidValidate = this.fieldDidValidate.bind(this);
      }
    
      componentWillMount() {
        this.context.form.addFieldWillValidateEventListener(this.fieldWillValidate);
        this.context.form.addFieldDidValidateEventListener(this.fieldDidValidate);
      }
    
      componentWillUnmount() {
        this.context.form.removeFieldWillValidateEventListener(this.fieldWillValidate);
        this.context.form.removeFieldDidValidateEventListener(this.fieldDidValidate);
      }
    
      fieldWillValidate(fieldName) {
        if (fieldName === this.props.name) this.setState({field: undefined});
      }
    
      fieldDidValidate(field) {
        if (field.name === this.props.name) this.setState({field});
      }
    
      handle_change(e) {
        var new_text = e.currentTarget.value;
        this.props.on_Input_Change(e, new_text);
      }
    
      render() {
        const { field } = this.state;
        let className = 'adaptive_placeholder_input_container';
        if (field !== undefined) {
          if (field.hasErrors()) className += ' error';
          if (field.hasWarnings()) className += ' warning';
        }
    
        return (
          
    ); } } class Form extends React.Component { constructor(props) { super(props); this.state = { Name: 'No Name', Value_1: '0', Value_2: '0', Display_Value: '0' }; } handle_text_input(e, new_text) { this.form.validateFields(e.currentTarget); this.setState({ Name: new_text }); } handle_value_1_input(e, new_value) { this.form.validateFields(e.currentTarget); if (this.form.isValid()) { new_value = parseInt(new_value); var updated_display = new_value + parseInt(this.state.Value_2); updated_display = updated_display.toString(); this.setState({ Value_1: new_value, Display_Value: updated_display }); } else { this.setState({ Display_Value: 'Error' }); } } handle_value_2_input(e, new_value) { this.form.validateFields(e.currentTarget); if (this.form.isValid()) { new_value = parseInt(new_value); var updated_display = parseInt(this.state.Value_1) + new_value; updated_display = updated_display.toString(); this.setState({ Value_2: new_value, Display_Value: updated_display }); } else { this.setState({ Display_Value: 'Error' }); } } render() { return(

    Name: {this.state.Name}

    Value 1 + Value 2 = {this.state.Display_Value}

    this.form = form} noValidate> !/^\w+$/.test(value)} warning>Should only contain alphanumeric characters
    ); } } ReactDOM.render(, document.getElementById('app'));

    The proposed solution here is hackish as I've tried to keep it close to the original jsfiddle. For proper form validation with react-form-with-constraints, check https://github.com/tkrotoff/react-form-with-constraints#examples

提交回复
热议问题