javascriptreactjsreact-final-formfinal-formreact-final-form-arrays

How to set the error in the array react-final-form


I have some problems with the errors in the react-final-form. I don't know how to set the error in the array. Could someone give me an example about that? Thanks.

Just set the validation for this example. https://codesandbox.io/s/kx8qv67nk5


Solution

  • You can add a validator directly to the Field (in its validate property) and this will be applied to the specific Field element in the array. For example with a validator called 'required' as in this example

    const required = value => (value ? undefined : "Required"); 
    

    Then the Field will look like this with the ability to access the metadata with any validation errors

    <Field 
         name={`${name}.firstName`}
         validate={ required }
         render={({ input, meta }) => (
           <div>
             <input {...input} />
             {meta.touched && meta.error && <span>{meta.error}</span>}
           </div>
         )} 
    />
    

    Working example :

    https://codesandbox.io/s/y3w6yo8xr9