How to properly validate input values with React.JS?

前端 未结 9 1061
孤城傲影
孤城傲影 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

    I recently spent a week studying lot of solutions to validate my forms in an app. I started with all the most stared one but I couldn't find one who was working as I was expected. After few days, I became quite frustrated until i found a very new and amazing plugin: https://github.com/kettanaito/react-advanced-form

    The developper is very responsive and his solution, after my research, merit to become the most stared one from my perspective. I hope it could help and you'll appreciate.

    0 讨论(0)
  • 2020-12-22 19:57

    I have written This library which allows you to wrap your form element components, and lets you define your validators in the format :-

    <Validation group="myGroup1"
        validators={[
                {
                 validator: (val) => !validator.isEmpty(val),
                 errorMessage: "Cannot be left empty"
                },...
            }]}>
                <TextField value={this.state.value}
                           className={styles.inputStyles}
                           onChange={
                            (evt)=>{
                              console.log("you have typed: ", evt.target.value);
                            }
                           }/>
    </Validation>
    
    0 讨论(0)
  • 2020-12-22 19:58

    I have used redux-form and formik in the past, and recently React introduced Hook, and i have built a custom hook for it. Please check it out and see if it make your form validation much easier.

    Github: https://github.com/bluebill1049/react-hook-form

    Website: http://react-hook-form.now.sh

    with this approach, you are no longer doing controlled input too.

    example below:

    import React from 'react'
    import useForm from 'react-hook-form'
    
    function App() {
      const { register, handleSubmit, errors } = useForm() // initialise the hook
      const onSubmit = (data) => { console.log(data) } // callback when validation pass
    
      return (
        <form onSubmit={handleSubmit(onSubmit)}>
          <input name="firstname" ref={register} /> {/* register an input */}
    
          <input name="lastname" ref={register({ required: true })} /> {/* apply required validation */}
          {errors.lastname && 'Last name is required.'} {/* error message */}
    
          <input name="age" ref={register({ pattern: /\d+/ })} /> {/* apply a Refex validation */}
          {errors.age && 'Please enter number for age.'} {/* error message */}
    
          <input type="submit" />
        </form>
      )
    }
    
    0 讨论(0)
提交回复
热议问题