简体   繁体   English

如何在数组react-final-form中设置错误

[英]How to set the error in the array react-final-form

I have some problems with the errors in the react-final-form. 我对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 https://codesandbox.io/s/kx8qv67nk5

您可以在提交之前和提交之后添加验证,例如https://codesandbox.io/s/8xkn4r10m8

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. 您可以将验证器直接添加到Field(在其validate属性中),它将应用于数组中的特定Field元素。 For example with a validator called 'required' as in this example 例如,在此示例中,使用名为“ required”的验证程序

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 https://codesandbox.io/s/y3w6yo8xr9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM