我有适合你的解决方案,我建议使用 Saga 和 HttpError。
首先,从我们的 dataProvider 我们需要抛出HttpError
这样的:
...
import {HttpError} from 'react-admin';
...
...
// Make the request with fetch/axios whatever you prefer and catch the error:
// message - the message that will appear in the alert notification popup
// status - the status code
// errors - the errors in key => value format, example in comment below
return fetchClient.request(config).then((response) => {
return convertHTTPResponse(response, type, resource, params);
}).catch(function (error) {
throw new HttpError(error.response.data.message, error.response.status, error.response.data.errors);
});
然后像这样创建传奇:
import {CRUD_CREATE_FAILURE} from "react-admin";
import {stopSubmit} from 'redux-form';
import {put, takeEvery} from "redux-saga/effects";
export default function* errorSagas() {
yield takeEvery(CRUD_CREATE_FAILURE, crudCreateFailure);
}
export function* crudCreateFailure(action) {
var json = action.payload;
// json structure looks like this:
// {
// username: "This username is already taken",
// age: "Your age must be above 18 years old"
// }
yield put(stopSubmit('record-form', json));
}
请确保错误(json)的格式与上例相同!
然后将 saga 插入组件中:
import errorSagas from './sagas/errorSagas';
...
...
<Admin
customSagas={[ errorSagas ]}
loginPage={LoginPage}
authProvider={authProvider}
dataProvider={dataProvider}
>
繁荣!有用
祝你好运!