我对react-final-form中的错误有一些问题。我不知道如何在数组中设置错误。有人可以给我一个例子吗?感谢。
只需为此示例设置验证。 https://codesandbox.io/s/kx8qv67nk5
答案 0 :(得分:1)
您可以在提交前和提交后添加验证,例如https://codesandbox.io/s/8xkn4r10m8
答案 1 :(得分:0)
您可以将验证器直接添加到Field(在其validate属性中),这将应用于数组中的特定Field元素。例如,在本例中使用名为“required”的验证器
const required = value => (value ? undefined : "Required");
然后,Field将具有使用任何验证错误访问元数据的能力
<Field
name={`${name}.firstName`}
validate={ required }
render={({ input, meta }) => (
<div>
<input {...input} />
{meta.touched && meta.error && <span>{meta.error}</span>}
</div>
)}
/>
工作示例: