59971

Show server-side validation errors after failed form submit

Question:

How can I show validation messages after failed form submit? API request returns HTTP 400 'application/problem+json' response and contains violations as a list with field path.

<a href="https://tools.ietf.org/html/rfc7807#section-3" rel="nofollow">https://tools.ietf.org/html/rfc7807#section-3</a>

{ "type": "https://example.net/validation-error", "title": "Your request parameters didn't validate.", "invalid-params": [ { "name": "age", "reason": "must be a positive integer" }, { "name": "color", "reason": "must be 'green', 'red' or 'blue'" } ] }

Answer1:

I have the solution for you, I'd recommend to do it with Saga and HttpError.

First, from our dataProvider we need to throw the HttpError like this:

... 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); });

Then create saga like that:

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)); }

Please make sure the errors (json) is in the format like in the example above!

Then insert the saga in the componenet:

import errorSagas from './sagas/errorSagas'; ... ... <Admin customSagas={[ errorSagas ]} loginPage={LoginPage} authProvider={authProvider} dataProvider={dataProvider} >

Boom! it works <a href="https://i.stack.imgur.com/GMZli.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/GMZli.png" data-original="https://i.stack.imgur.com/GMZli.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Good luck!

Recommend

  • “Too few positionals” in macro definition
  • Node.js - Configuring $NODE_PATH with NVM
  • Oracle APEX | Validate Tabular form Row by Row
  • Oracle MERGE: only NOT MATCHED is triggered
  • Azure deployment virtual directory [duplicate]
  • Combine solr's document score with a static, indexed score
  • Populate ListView with ArrayList having String array as elements
  • How to make add replies to comments in Django?
  • java 8 parallel stream Issue
  • DB2 Unique Constraint over multiple Columns
  • How to access WPF class library from Silverlight using iron python. Is it possible?
  • Fork and Join with Amazon Lambda
  • PHPMailer do not work properly sometimes on gmail port 465 or 587 on localhost
  • Migrating MOSS 2007 from SQL 2000 to SQL 2005 [closed]
  • Newtonsoft.json serializing and deserializing base/inheirited where classes are from shared projects
  • Open a cmd program with full functionality (i/o)
  • Special characters (ë) in JSON-response
  • Finding all XML nodes between each two processing instructions
  • Git for windows has stopped working
  • Web scraping pdf files from HTML
  • Use 2D Text into 3D scenes in JavaFX results in blurry texts
  • Slick: How can I combine a SQL LIKE statement with a SQL IN statement
  • Auto send email based on the time and email address in database
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • Android studio import problems. (Apktool)
  • Adding horizontal slider to QTableWidget
  • Use AutoIt with java applications
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Arraylist of strings into one comma separated string
  • How to select multiple items from a List view - JavaFX 8
  • Ruby on Rails: Get mediaplayer information (iTunes, TRAKTOR, Cog; current song + playlist)
  • ASP.NET MVC 2 actions for the same route?
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • How to clear a browser cache in Protractor
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Google App Engine Datastore: Dealing with eventual consistency
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • ssh remote server login script
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • How to call different template for different category archive page in woocommerce