字段验证如果`react-admin`的`SimpleForm`具有验证函数,则不会被调用。

huangapple go评论48阅读模式
英文:

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 &lt;SimpleForm/&gt; I suggest that you verify on the SimpleForm validate that the values are set and remove the fields required validate={requiredField}

huangapple
  • 本文由 发表于 2023年6月12日 19:54:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456410-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定