Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Making Sure Your Forms Don't Suck

Making Sure Your Forms Don't Suck

Damian Nicholson

October 23, 2017
Tweet

More Decks by Damian Nicholson

Other Decks in Technology

Transcript

  1. const fieldAlias = 'userLogin'; this.setState({ [fieldAlias]: { ...this.state[fieldAlias], value: changeFn('[email protected]'),

    touched: true, } }, () => { Promise.all(validations.map((fn) => { return fn(this.state.values, this.serverErrors(), fieldAlias, 'onChange'); })) .then(() => { this.setValid() }) .catch((errors) => { this.setInvalid(errors) }); });
  2. const validationFn = (values, serverErrors = {}, fieldUpdated, eventPhase) {

    return new Promise((resolve, reject) => { const errors = {}; if (!values.userLogin) { errors.userLogin = 'Please enter your email address'; } Object.assign(errors, serverErrors); if (Object.keys(errors).length === 0) { return resolve(errors); } return reject(errors); }); }; CONDITIONAL VALIDATIONS DEPENDANT ON OTHER FIELDS
  3. const SignInForm = ({ ...props }) => { const {

    handleChange, handleBlur, form, method, fields, ...other } = props; const { userLogin, password } = form; return ( <form method={method} action="POST" {...other}> <input type="text" data-alias="userLogin" onChange={handleChange} onBlur={handleBlur} value={userLogin.value} /> {userLogin.touched && userLogin.errors.length > 0 && <span>{userLogin.errors}</span> } ... <button>Sign in</button> </form> ); }; export default conferizeForm(SignInForm, validationFn);
  4. Consolidated event handling - either onChange or onBlur Conditional validations

    Sync and async validations declared and resolved using Promise API Composable validations Developers own their form and it's fields Synchronising server and client side validations