I want to use Redux-form in a manner that changes input color & displays the actual error on top of the page. How do I access the list of current field errors outside the fields themselves?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
You can't get the list of errors from outside of the render function given to the Field component. This is because errors are not stored in the redux store.
EDIT 26/12/2018 :
This answer is taking some age. ReduxForm now stores errors in the Redux store. Take a look to @nicoqh's answer which is using ReduxForm's selectors to get errors in any Redux connected component.
This answer is not totaly obsolete but it's not anymore the cleanest way to achieve this imho.
Solution 1: Use multiple Field for the same value
The first solution is to use multiple instance of Field for the same value. If multiple Field components have the same name and is connected to the same form name, they will all be connected to the same value and the same error handling.
So you can use a Field component and only render the error.
Solution 2: Use the global error prop
A second solution is to use the global error props, but you will have to display the errors from the container component using
reduxForm
.Pay attention that this is a total antipatern ! Global error prop is for field independent errors.
Solution 3: Get errors from the store
You always can get errors from the store by applying your validate function on the value presents in the store. It can be not performant for heavy validation because it run through validation at each render, so it runs twice when a value change and one for nothing if some other props changes. It can also be dificult to do with async validation.
EDIT 26/12/2018 :
This last "solution" wasn't a good one at the time I've posted it. But since
componentWillReceiveProps
is deprecated in React, it's not a solution at all. Please don't do this in you application. I don't delete this for history in case this answer was linked somewhere.You can use the state selectors provided by redux-form.
In particular,
getFormSubmitErrors
will give you the submit validation errors:The original, unconnected
MyForm
component might look like this:If you want to display the synchronous validation errors, you can use the
getFormSyncErrors
selector instead.