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

Commit 484f213

Browse files
committed
Simplifying form validation
1 parent fadca5d commit 484f213

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed

examples/quick-start/components/user-form.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ class UserForm extends React.Component {
3737
render() {
3838
const { forms: { user }, dispatch } = this.props;
3939

40-
return (
41-
<LocalForm onSubmit={v=>console.log(v)}>
42-
<Control.text model=".username" />
43-
</LocalForm>
44-
)
45-
4640
return (
4741
<Form model="user" onSubmit={this.handleSubmit.bind(this)}>
4842
<div>
4943
<label>First name:</label>
50-
<Control.text model="user.firstName" validators={{len: (val) => val.length > 8}} />
44+
<Control.text
45+
model="user.firstName"
46+
validators={{len: (val) => val.length > 8}}
47+
mapProps={{
48+
className: ({fieldValue}) => fieldValue.focus
49+
? 'focused'
50+
: ''
51+
}}
52+
/>
5153
<Errors model=".firstName" messages={{
5254
len: 'len must be > 8'
5355
}} />

src/components/form-component.js

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import omit from '../utils/omit';
88

99
import actions from '../actions';
1010
import getValidity from '../utils/get-validity';
11-
import invertValidity from '../utils/invert-validity';
1211
import invertValidators from '../utils/invert-validators';
12+
import isValidityInvalid from '../utils/is-validity-invalid';
1313
import getForm from '../utils/get-form';
1414
import getModel from '../utils/get-model';
1515
import getField from '../utils/get-field';
@@ -148,33 +148,13 @@ function createFormClass(s = defaultStrategy) {
148148
const validatorsChanged = validators !== this.props.validators
149149
|| errors !== this.props.errors;
150150

151-
let validityChanged = false;
152-
153-
const fieldsValidity = mapValues(validators, (validator, field) => {
154-
const nextValue = field
155-
? s.get(nextProps.modelValue, field)
156-
: nextProps.modelValue;
151+
const errorValidators = validators
152+
? merge(invertValidators(validators), errors)
153+
: errors;
157154

158-
const currentValue = field
159-
? s.get(modelValue, field)
160-
: modelValue;
161-
162-
const currentValidity = getField(formValue, field).validity;
163-
164-
if ((!initial && !validatorsChanged) && (nextValue === currentValue)) {
165-
return currentValidity;
166-
}
167-
168-
const fieldValidity = getValidity(validator, nextValue);
169-
170-
if (!shallowEqual(fieldValidity, currentValidity)) {
171-
validityChanged = true;
172-
}
173-
174-
return fieldValidity;
175-
});
155+
let validityChanged = false;
176156

177-
const fieldsErrorsValidity = mapValues(errors, (errorValidator, field) => {
157+
const fieldsErrors = mapValues(errorValidators, (errorValidator, field) => {
178158
const nextValue = field
179159
? s.get(nextProps.modelValue, field)
180160
: nextProps.modelValue;
@@ -198,14 +178,11 @@ function createFormClass(s = defaultStrategy) {
198178
return fieldErrors;
199179
});
200180

201-
const fieldsErrors = merge(
202-
invertValidity(fieldsValidity),
203-
fieldsErrorsValidity
204-
);
205-
206181
// Compute form-level validity
207-
if (!fieldsValidity.hasOwnProperty('') && !fieldsErrorsValidity.hasOwnProperty('')) {
182+
if (!fieldsErrors.hasOwnProperty('')) {
208183
fieldsErrors[''] = false;
184+
validityChanged = validityChanged
185+
|| isValidityInvalid(formValue.$form.errors);
209186
}
210187

211188
if (validityChanged) {

0 commit comments

Comments
 (0)