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

Designing form validation the right way

Designing form validation the right way

Damian Nicholson

January 19, 2018
Tweet

More Decks by Damian Nicholson

Other Decks in Programming

Transcript

  1. Add an event • Speakers • Sessions • Sponsors •

    About • Tickets • Tax and invoice data
  2. Major E-Commerce sites containing inline validation in checkout 40% 60%

    https://baymard.com/blog/inline-form-validation
  3. "Inline Validation in Web Forms" https://alistapart.com/article/inline-validation-in-web-forms 3 groups of participants

    1.No inline validation - control version 2.Poorly implemented inline validation 3.Well implemented inline validation
  4. "Inline Validation in Web Forms" • Accurate • Faster •

    Confident • Satisfied https://alistapart.com/article/inline-validation-in-web-forms
  5. How many of you have been shown an error message

    either before or while updating a field and been left confused?
  6. Recap • Inline validations are necessary to enhancing our users

    journey and making them more confident and happy • <50% of major online E-Commerce players either aren't doing it or are doing it incorrectly • Losing money • Alienating users
  7. Prior art • jQuery validation • AngularJS built in validation

    • HTML5 validation • redux-form* • ...
  8. #1 Managing state • Low level or poor APIs e.g.

    HTML5 validate API • Generally a one-to-one mapping between fields and validations • Different fields respond to different event life-cycles for capturing their values across different types of validations e.g. onChange, onBlur, onKeypress • Poor support for adding validations dynamically if (country = 'UK') { // do post code validation } • Poor support for asynchronous field validation and how to reconcile these with synchronous validations
  9. #2 Poor styling support / inflexible • HTML5 field validation

    messages aren't able to be styled consistently and are wrong • All in one solutions require you to use their form fields which can be a huge burden and means your bound by their styling output as to how your fields look
  10. #3 Constraint alignment • Only possible for very basic field

    validations and messages • JavaScript based stack alleviates that to a degree • Most other solutions basically copy and codify constraints and error messages so are prone to misalignment over time • Impossible to cover all use cases regardless of stack e.g. username uniqueness, date overlaps
  11. #4 Constraint reconciliation • How to reconcile errors generated from

    the server and any subsequent errors on the client? • What takes precedence when?
  12. 4 key challenges 1. Handling user driven state changes effectively

    2. Poor styling support and no control over markup 3. Impossible to align backend data constraints and error messages with the on the client 4. Accept server side validations exist always and how to reconcile them
  13. 4 key challenges 1. Handling user driven state changes effectively

    2. Poor styling support and no control over markup 3. Impossible to align backend data constraints and error messages with the on the client 4. Accept server side validations exist always and how to reconcile them
  14. React • Rendered HTML is colocated with events it makes

    attaching these easier • Makes handling UI state and as an extension of that dynamic form state a breeze • Smoothes over browser quirks for event lifecycles e.g. onChange • Heavily favours composition • Controlled forms have a consistent easy to use API
  15. Key point is that ConferizeForm* holds all the form state

    which is passed down to your component as props
  16. Composable validations • Pass an array of validation functions or

    just one function here, so you can compose them using ES6 imports • The validation functions are run in the order they're passed in • Validations bail out as soon as the first one returns any errors https://codesandbox.io/s/jnyv95r26w
  17. Form level validation context • Allows you as the developer

    to work at the form level context for handling validations as you get the entire form state to work with and inspect • You also get access to the eventPhase as these validations are run both onChange and onBlur https://codesandbox.io/s/jnyv95r26w
  18. Reconcile server and client side validations • Store the server

    side validation message and value which prompted the message • Accessible through initialErrors in validation function - developers can do what they like with it - choose to use it or ignore it
  19. 4 key challenges 1. Handling user driven state changes effectively

    2. Poor styling support and no control over markup 3. Impossible to align backend data constraints and error messages with the on the client 4. Accept server side validations exist always and how to reconcile them
  20. 4 key challenges 1. Handling user driven state changes effectively

    2. Poor styling support and no control over markup 3. Impossible to align backend data constraints and error messages with the on the client 4. Accept server side validations exist always and how to reconcile them
  21. 4 key challenges 1. Handling user driven state changes effectively

    2. Poor styling support and no control over markup 3. Impossible to align backend data constraints and error messages with the on the client 4. Accept server side validations exist always and how to reconcile them
  22. 4 key challenges 1. Handling user driven state changes effectively

    2. Poor styling support and no control over markup 3. Impossible to align backend data constraints and error messages with the on the client 4. Accept server side validations exist always and how to reconcile them
  23. Use feedback while a user is filling out a field

    only in certain scenarios but use an appropriate delay e.g. username uniqueness, password confirmation
  24. If your unsure where to add inline validations, focus more

    on fields which require some user thought to fill out