英文:
Field validations don't get called if react-admin SimpleForm has a validate function
问题
我们注意到,如果SimpleForm中定义了validate函数(如上所示),则字段中的validate函数不会被调用。这是否是预期的react-admin SimpleForm行为?是否有一种同时进行表单级和字段级验证的方法?
英文:
Consider the following code:
const validateLetterTypeForm = (values: Partial<ILetterType>) => {
const errors: Partial<ILetterType> = {};
//TODO: put code to validate form here
//https://marmelab.com/react-admin/Validation.html#validation-mode
return errors;
};
const LetterTypeForm2 = () => {
return (
<SimpleForm noValidate validate={validateLetterTypeForm}>
<TextInput source="businessArea" required validate={requiredField}/>
<TextInput source="letterTypeDesc" required validate={requiredField}/>
</SimpleForm>
);
};
We noticed that, if SimpleForm has a validate function defined (as above), the validate functions in the fields do not get called. Is this an expected react-admin SimpleForm behavior? Is there a way to have both form and field level validations?
答案1
得分: 2
自从 react-admin 4.X.X
版本开始,您不能同时进行两种验证。
https://marmelab.com/react-admin/Validation.html
有多种方法可以同时进行两种验证,但在所有情况下都没有正常工作的方法。
如果您只有这个 <SimpleForm/>
,我建议您在 SimpleForm 的验证中验证值是否已设置,并移除字段 required validate={requiredField}
。
英文:
Since react-admin 4.X.X
you cannot have both validations.
https://marmelab.com/react-admin/Validation.html
They are multiple workaround to do both but none works correctly in all situations.
If you only have this <SimpleForm/>
I suggest that you verify on the SimpleForm validate that the values are set and remove the fields required validate={requiredField}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论