<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Perry Trinier</title>
    <description>The latest articles on Forem by Perry Trinier (@ptrin).</description>
    <link>https://forem.com/ptrin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F162592%2F0499c3e6-0f98-4b0b-a2cd-a28e019a08e7.jpg</url>
      <title>Forem: Perry Trinier</title>
      <link>https://forem.com/ptrin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ptrin"/>
    <language>en</language>
    <item>
      <title>Accessible Formik forms</title>
      <dc:creator>Perry Trinier</dc:creator>
      <pubDate>Wed, 16 Oct 2019 02:27:45 +0000</pubDate>
      <link>https://forem.com/ptrin/accessible-formik-forms-2gld</link>
      <guid>https://forem.com/ptrin/accessible-formik-forms-2gld</guid>
      <description>&lt;p&gt;&lt;a href="https://jaredpalmer.com/formik/" rel="noopener noreferrer"&gt;Formik&lt;/a&gt; is a popular option for creating forms in React because it simplifies handling form state, submission, validation, etc. It's also very flexible, allowing you to customize and override the behaviour of its components according to your needs. &lt;/p&gt;

&lt;p&gt;This is useful because we need to tie into some of these features to build a form that works for all users.&lt;/p&gt;

&lt;p&gt;Who benefits from these changes?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users of screen readers who are informed of errors&lt;/li&gt;
&lt;li&gt;Magnification users who may have their software configured to follow the keyboard focus&lt;/li&gt;
&lt;li&gt;People who have chronic pain or for whom it would be a burden to backtrack through the form manually e.g. using a switch system&lt;/li&gt;
&lt;li&gt;All keyboard users who can work more efficiently&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's dive into it.&lt;/p&gt;

&lt;h2&gt;
  
  
  A basic Formik form
&lt;/h2&gt;

&lt;p&gt;This simple example of a Formik form has two fields and a submit button, and displays error messages when there are validation errors.&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/qy93o"&gt;
&lt;/iframe&gt;
&lt;br&gt;
It's important to note that these error messages are not programmatically associated to form fields, so screen reader users would not be informed that there are errors with the input.&lt;/p&gt;
&lt;h2&gt;
  
  
  State and error message
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.w3.org/WAI/WCAG21/Techniques/aria/ARIA21.html#examples" rel="noopener noreferrer"&gt;When a field has an error, we need to add &lt;code&gt;aria-invalid="true"&lt;/code&gt;&lt;/a&gt; so that screen readers will announce that it is invalid. We also need to associate the field with its error message. We can do that by giving the error message wrapper a unique &lt;code&gt;id&lt;/code&gt; attribute and adding &lt;code&gt;aria-describedby=[error wrapper id]&lt;/code&gt; to the field when it has an error.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
I've also added &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-required_attribute" rel="noopener noreferrer"&gt;the &lt;code&gt;aria-required&lt;/code&gt; attribute&lt;/a&gt;.

&lt;p&gt;Here's the original example again with these changes made:&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/zm6jp"&gt;
&lt;/iframe&gt;
&lt;br&gt;
Screen readers will now correctly announce that the field is invalid, and read the error message after the label of the field.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F7W3Rj5l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F7W3Rj5l.png" alt="Screenshot of VoiceOver announcing field label and invalid state of field"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FzPl9YHB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FzPl9YHB.png" alt="Screenshot of VoiceOver announcing error message content after label and state"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Focus handling
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ytimg.com%2Fvi%2Fk5Cfhooa1bI%2Fhqdefault.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ytimg.com%2Fvi%2Fk5Cfhooa1bI%2Fhqdefault.jpg" alt="A meme of Fred Willard's character in "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now our form fields are properly marked as invalid and associated with their error messages, but a screen reader user would not be immediately informed that there are validation errors when submitting the form. A user &lt;em&gt;could&lt;/em&gt; tab backwards to check whether any fields have errors, but we can improve the experience for everyone by focusing the first field with an error when there are validation errors.&lt;/p&gt;

&lt;p&gt;To this end, I was happy to find the &lt;a href="https://gist.github.com/dphrag/4db3b453e02567a0bb52592679554a5b" rel="noopener noreferrer"&gt;&lt;code&gt;ErrorFocus&lt;/code&gt; component&lt;/a&gt; by &lt;a href="https://twitter.com/nigelfish" rel="noopener noreferrer"&gt;Nigel Fish&lt;/a&gt; in a &lt;a href="https://github.com/jaredpalmer/formik/issues/146#issuecomment-435226018" rel="noopener noreferrer"&gt;Formik github issue&lt;/a&gt;. Place &lt;code&gt;&amp;lt;ErrorFocus /&amp;gt;&lt;/code&gt; inside of &lt;code&gt;&amp;lt;Formik&amp;gt;&lt;/code&gt; where it will have access to the Formik props. You might also consider using the &lt;a href="https://palmer.net/blog/form-field-scroll-management-in-react" rel="noopener noreferrer"&gt;Palmer Group's approach for Form Field Scroll Management&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I could &lt;a href="https://webaim.org/techniques/formvalidation/#error" rel="noopener noreferrer"&gt;set focus to a summary of errors at the top of the form&lt;/a&gt; instead, but for a simple form such as this, it doesn't feel like the right choice.&lt;/p&gt;
&lt;h2&gt;
  
  
  Focusing the first field when the form loads
&lt;/h2&gt;

&lt;p&gt;In my case, when a user starts editing the form they want to start at the beginning of the form and work through it. So let's focus the first field when the form is rendered. We can do this by using a React &lt;code&gt;ref&lt;/code&gt;, which Formik provides access to via the &lt;code&gt;innerRef&lt;/code&gt; property of &lt;code&gt;&amp;lt;Field&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Finished example
&lt;/h2&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/hhw1g"&gt;
&lt;/iframe&gt;
&lt;br&gt;
As someone working on a product primarily used by people with disabilities, ensuring form accessibility is a standard part of my development process. I hope this example shows that with the declarative style of React and Formik, it's pretty straightforward to make your front end validation accessible. &lt;/p&gt;

&lt;p&gt;I'd love to know how you approach this – let me know in the comments :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>a11y</category>
    </item>
  </channel>
</rss>
