- Redux Form
- Examples
- Field-Level Validation Example
Field-Level Validation Example
As well as allowing you to provide a validation function to validate all the
values in your form at once (see
Synchronous Validation Example),
you may also provide individual value validation functions for each Field
or
FieldArray
.
The parameters to the validation function are:
value
- The current value of the fieldallValues
- The values of the entire formprops
- Any props passed to the form
If the value
is valid, the validation function should return undefined
.
If the value
is invalid, the validation function should return an error. This
is usually a string, but it does not have to be.
Obviously, you will want to define your validation functions just once for your application and reuse them as needed across your forms.
Running this example locally
To run this example locally on your machine clone the redux-form
repository,
then cd redux-form
to change to the repo directory, and run npm install
.
Then run npm run example:fieldLevelValidation
or manually run the following
commands:
cd ./examples/fieldLevelValidation
npm install
npm start
Form
Values
undefined
Code
FieldLevelValidationForm.js
import React from 'react'
import { Field, reduxForm } from 'redux-form'
const required = value => (value ? undefined : 'Required')
const maxLength = max => value =>
value && value.length > max ? `Must be ${max} characters or less` : undefined
const maxLength15 = maxLength(15)
export const minLength = min => value =>
value && value.length < min ? `Must be ${min} characters or more` : undefined
export const minLength2 = minLength(2)
const number = value =>
value && isNaN(Number(value)) ? 'Must be a number' : undefined
const minValue = min => value =>
value && value < min ? `Must be at least ${min}` : undefined
const minValue18 = minValue(18)
const email = value =>
value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
? 'Invalid email address'
: undefined
const tooOld = value =>
value && value > 65 ? 'You might be too old for this' : undefined
const aol = value =>
value && /.+@aol\.com/.test(value)
? 'Really? You still use AOL for your email?'
: undefined
const alphaNumeric = value =>
value && /[^a-zA-Z0-9 ]/i.test(value)
? 'Only alphanumeric characters'
: undefined
export const phoneNumber = value =>
value && !/^(0|[1-9][0-9]{9})$/i.test(value)
? 'Invalid phone number, must be 10 digits'
: undefined
const renderField = ({
input,
label,
type,
meta: { touched, error, warning }
}) => (
<div>
<label>{label}</label>
<div>
<input {...input} placeholder={label} type={type} />
{touched &&
((error && <span>{error}</span>) ||
(warning && <span>{warning}</span>))}
</div>
</div>
)
const FieldLevelValidationForm = props => {
const { handleSubmit, pristine, reset, submitting } = props
return (
<form onSubmit={handleSubmit}>
<Field
name="username"
type="text"
component={renderField}
label="Username"
validate={[required, maxLength15, minLength2]}
warn={alphaNumeric}
/>
<Field
name="email"
type="email"
component={renderField}
label="Email"
validate={email}
warn={aol}
/>
<Field
name="age"
type="number"
component={renderField}
label="Age"
validate={[required, number, minValue18]}
warn={tooOld}
/>
<Field
name="phone"
type="number"
component={renderField}
label="Phone number"
validate={[required, phoneNumber]}
/>
<div>
<button type="submit" disabled={submitting}>
Submit
</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>
Clear Values
</button>
</div>
</form>
)
}
export default reduxForm({
form: 'fieldLevelValidation' // a unique identifier for this form
})(FieldLevelValidationForm)