Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 46ed9b4

Browse files
committed
Added support for displaying custom messages in <Errors> component (WIP)
1 parent 2d9d333 commit 46ed9b4

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

src/components/errors-component.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,28 @@ import isArray from 'lodash/isArray';
88

99
import { getFieldFromState } from '../utils';
1010

11-
function createErrorComponent(component, message, key) {
11+
function createErrorComponent(component, message, modelValue, key) {
12+
const messageString = typeof message === 'function'
13+
? message(modelValue)
14+
: message;
15+
1216
return React.createElement(
1317
component,
1418
{ key },
15-
message);
19+
messageString);
1620
}
1721

18-
function mapErrorMessages(errors, messages, component) {
22+
function mapErrorMessages(errors, messages, modelValue, component) {
1923
return compact(map(errors, (error, key) => {
2024
const message = messages[key];
2125

2226
if (error) {
2327
if (message) {
24-
return createErrorComponent(component, message, key);
28+
return createErrorComponent(component, message, modelValue, key);
2529
} else if (typeof error === 'string') {
26-
return createErrorComponent(component, error, key);
30+
return createErrorComponent(component, error, modelValue, key);
2731
} else if (typeof error === 'object') {
28-
return mapErrorMessages(error, messages, component);
32+
return mapErrorMessages(error, messages, modelValue, component);
2933
}
3034
}
3135

@@ -55,7 +59,7 @@ class Errors extends Component {
5559
render() {
5660
const {
5761
// model,
58-
// modelValue,
62+
modelValue,
5963
fieldValue,
6064
fieldValue: {
6165
valid,
@@ -73,7 +77,7 @@ class Errors extends Component {
7377

7478
const errorMessages = valid
7579
? null
76-
: mapErrorMessages(errors, messages, component);
80+
: mapErrorMessages(errors, messages, modelValue, component);
7781

7882
return React.createElement(
7983
wrapper,

test/errors-component-spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,46 @@ describe('<Errors />', () => {
117117
});
118118
});
119119

120+
describe('displaying custom messages', () => {
121+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
122+
testForm: formReducer('test', {}),
123+
test: modelReducer('test'),
124+
}));
125+
126+
const form = TestUtils.renderIntoDocument(
127+
<Provider store={store}>
128+
<form>
129+
<Errors model="test.foo"
130+
messages={{
131+
length: (val) => `${val && val.length} chars is too short`,
132+
}}
133+
/>
134+
<Field model="test.foo"
135+
validators={{
136+
length: (v) => v && v.length && v.length > 5,
137+
}}
138+
>
139+
<input type="text" />
140+
</Field>
141+
</form>
142+
</Provider>
143+
);
144+
145+
const input = TestUtils.findRenderedDOMComponentWithTag(form, 'input');
146+
147+
it('should return messages from functions called with the model value', () => {
148+
input.value = 'four';
149+
150+
TestUtils.Simulate.change(input);
151+
152+
const errors = TestUtils.scryRenderedDOMComponentsWithTag(form, 'span');
153+
154+
assert.lengthOf(errors, 1);
155+
156+
assert.equal(errors[0].innerHTML, '4 chars is too short');
157+
});
158+
});
159+
120160
describe('the "show" prop', () => {
121161
function renderErrorsWithShow(show) {
122162
const store = applyMiddleware(thunk)(createStore)(combineReducers({

0 commit comments

Comments
 (0)