Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- import { Field, reduxForm } from 'redux-form';
- function validate(values) {
- const errors = {}
- if (!values.firstName) {
- errors.firstName = 'Required'
- }
- if (!values.lastName) {
- errors.lastName = 'Required'
- }
- if (!values.email) {
- errors.email = 'Required'
- }
- return errors
- }
- const renderField = ({
- input,
- label,
- type,
- meta: { touched, error }
- }) => (
- <div>
- <label>{label}</label>
- <div>
- <input {...input} placeholder={label} type={type} />
- {touched && error && <span>{error}</span>}
- </div>
- </div>
- )
- const SimpleForm = props => {
- const { handleSubmit, pristine, reset, submitting } = props;
- return (
- <form onSubmit={handleSubmit}>
- <div>
- <label>First Name</label>
- <div>
- <Field
- name="firstName"
- component={renderField}
- type="text"
- />
- </div>
- </div>
- <div>
- <label>Last Name</label>
- <div>
- <Field
- name="lastName"
- component={renderField}
- type="text"
- />
- </div>
- </div>
- <div>
- <label>Email</label>
- <div>
- <Field
- name="email"
- component={renderField}
- type="email"
- />
- </div>
- </div>
- <div>
- <label>Sex</label>
- <div>
- <label>
- <Field name="sex" component="input" type="radio" value="male" />
- {' '}
- Male
- </label>
- <label>
- <Field name="sex" component="input" type="radio" value="female" />
- {' '}
- Female
- </label>
- </div>
- </div>
- <div>
- <label>Favorite Color</label>
- <div>
- <Field name="favoriteColor" component="select">
- <option />
- <option value="ff0000">Red</option>
- <option value="00ff00">Green</option>
- <option value="0000ff">Blue</option>
- </Field>
- </div>
- </div>
- <div>
- <label htmlFor="employed">Employed</label>
- <div>
- <Field
- name="employed"
- id="employed"
- component="input"
- type="checkbox"
- />
- </div>
- </div>
- <div>
- <label>Notes</label>
- <div>
- <Field name="notes" component="textarea" />
- </div>
- </div>
- <div>
- <button type="submit" disabled={pristine || submitting}>Submit</button>
- <button type="button" disabled={pristine || submitting} onClick={reset}>
- Clear Values
- </button>
- </div>
- </form>
- );
- };
- export default reduxForm({
- form: 'simple', // a unique identifier for this form
- validate
- })(SimpleForm);
Add Comment
Please, Sign In to add comment