leuction
leuction

Reputation: 585

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

Upvotes: 0

Views: 5659

Answers (2)

Arrararr
Arrararr

Reputation: 364

you can add validation before submit and after submit like this https://codesandbox.io/s/8xkn4r10m8

Upvotes: 0

Rocket Garden
Rocket Garden

Reputation: 1196

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

Upvotes: 0

Related Questions