有没有办法使用https://github.com/marmelab/react-admin包执行服务器端表单验证?
这是AdminCreate组件的代码.它向api发送创建请求.如果一切正常,Api将返回验证错误,状态码为422或状态码为200.
export class AdminCreate extends Component {
render() {
return <Create {...this.props}>
<SimpleForm>
<TextInput source="name" type="text" />
<TextInput source="email" type="email"/>
<TextInput source="password" type="password"/>
<TextInput source="password_confirmation" type="password"/>
<TextInput source="phone" type="tel"/>
</SimpleForm>
</Create>;
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是,如何从服务器发送的错误对象中分别显示每个字段的错误?以下是错误对象的示例:
{
errors: {name: "The name is required", email: "The email is required"},
message: "invalid data"
}
Run Code Online (Sandbox Code Playgroud)
先感谢您!
class SimpleForm extends Component {
handleSubmitWithRedirect = (redirect = this.props.redirect) =>
this.props.handleSubmit(data => {
dataProvider(CREATE, 'admins', { data: { ...data } }).catch(e => {
throw new …Run Code Online (Sandbox Code Playgroud)