<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nick Kozhukharenko on Medium]]></title>
        <description><![CDATA[Stories by Nick Kozhukharenko on Medium]]></description>
        <link>https://medium.com/@KozhukharenkoN?source=rss-ec02ef6e7920------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*zJngfyO0ehCrJHHDOEc26A.jpeg</url>
            <title>Stories by Nick Kozhukharenko on Medium</title>
            <link>https://medium.com/@KozhukharenkoN?source=rss-ec02ef6e7920------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 17:29:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@KozhukharenkoN/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React form validation with MobX]]></title>
            <link>https://medium.com/@KozhukharenkoN/react-form-validation-with-mobx-8ce00233ae27?source=rss-ec02ef6e7920------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ce00233ae27</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[mobx]]></category>
            <category><![CDATA[validation]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[forms]]></category>
            <dc:creator><![CDATA[Nick Kozhukharenko]]></dc:creator>
            <pubDate>Tue, 01 Nov 2016 19:47:27 GMT</pubDate>
            <atom:updated>2016-12-05T18:50:29.244Z</atom:updated>
            <content:encoded><![CDATA[<p>Handling an input from a user and validating it — it’s feature that is present in every SPA.</p><p>As usual, fortunately (or <strong>un</strong>fortunately — choose what you like more), in React community there is no conventional approach of doing this. For those who are using Redux, there are permanent leader — <a href="http://redux-form.com/">redux-forms</a>. But what to do if you are using MobX? We should search for the most suitable library on the vast Github or invent a wheel.</p><p>I come across the <a href="https://github.com/foxhound87/mobx-react-form">mobx-react-form</a> library. But after browsing a docs and a source code (it’s quite big, thought), I found out that it has too many features what I don’t need.</p><p>For now I need only next <strong>features:</strong></p><ol><li>Show Error under each filed if it isn’t valid (give to user an instant feedback)</li><li>Disable a submit button if a form is not valid.</li></ol><p>The implementation should fit the next <strong>criteria</strong>:</p><ol><li><strong>reduce repetition in form components</strong></li><li><strong>be simple and clear (less than 30 lines of code)</strong></li><li>use the full power of MobX</li><li>easy to scale</li></ol><p>As an example, we will do a validation for login form and then will try to extract a logic to a generic class. While a form isn’t filled correctly — a button is disabled.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*T3wgTXmt67YDUnCW4oQZ1Q.gif" /></figure><h4>Form input component</h4><p>Following a component-driven development approach let’s implement a stateless form-input component. The usage should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c825c911cd8c641a5fa5cc0bff59b106/href">https://medium.com/media/c825c911cd8c641a5fa5cc0bff59b106/href</a></iframe><p>The implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83427677b4d190ecf262370ad8af6bbf/href">https://medium.com/media/83427677b4d190ecf262370ad8af6bbf/href</a></iframe><p>This component is totally ‘controlled’ which means that</p><ul><li>the value is set with the props. The FormInput component can receive value and error. If the error is passed — it’s shown beneath input.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*EdEgy-6rF46dC95witUNlw.png" /></figure><ul><li>the form data is updated through an onFieldChange() handler. This callback is called each time with ‘field-name’ and ‘value’ arguments.</li></ul><h4>Login form model</h4><p>Second step — defining form model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b30f92e5c145034e1c2542c0f1067441/href">https://medium.com/media/b30f92e5c145034e1c2542c0f1067441/href</a></iframe><p>Pay attention to the form data is decorated by MobX@bservable decorator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4cbcf064f7be768b0659e257ebae1b0/href">https://medium.com/media/c4cbcf064f7be768b0659e257ebae1b0/href</a></iframe><p>To handle a validation itself we will use a <a href="https://github.com/skaterdav85/validatorjs">ValidatorJS</a> library. I choose it over several others for its declarativeness, small size and multilingual support.</p><p>The next important piece of the FormStore class is the onFieldChange() function. It should validate and update model by settings a new value to such fields as value, error and isValid.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66ebc45e526e3667ddd62a2a78c1a707/href">https://medium.com/media/66ebc45e526e3667ddd62a2a78c1a707/href</a></iframe><p>Notice that each field was extended with one more field — rule which will be consumed by a Validator.</p><p>onFieldChange() is the function that is to be called whenever the text in any of the form fields changes.</p><p>It calls theValidator and then updates the model — this.form</p><p>Then let’s create LoginForm — the form component for login with two inputs — one for email and another for a password. It will receive an observable form data and the onFieldChange callback.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc033b8426fbd470c5da136ca8ad77eb/href">https://medium.com/media/fc033b8426fbd470c5da136ca8ad77eb/href</a></iframe><p>We got a static ‘dump’ component</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/353/1*9tHOrzsTOmrLxRupEgs7hw.png" /></figure><p>And we can use it the next:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a90593d3f463490a5161a48c5bae472a/href">https://medium.com/media/a90593d3f463490a5161a48c5bae472a/href</a></iframe><p>Actually, we can even pass a whole instance of LoginStore to the LoginForm.</p><h4>Extracting common logic</h4><p>To make this login reusable we can extract onFieldChange()s to a separate class and then all other classes will inherit from it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d08bdcb72ee64c2b783d5d5ca5e766d2/href">https://medium.com/media/d08bdcb72ee64c2b783d5d5ca5e766d2/href</a></iframe><p>getFlattenedValues() turns</p><pre>{<br>  fields: {<br>    email: {value: ‘val’}<br>  }<br>}</pre><p>into</p><pre>{ email: &#39;val }</pre><p>We can call setError() action to set a generic form error.</p><p>It’s useful if we want to display some async error, e.g.</p><pre>post(&#39;<a href="http://localhost:3001/">http://localhost:300</a>&#39;)<br>  .then(() =&gt; /* your code*/ )<br>  catch((e) =&gt; form.setError(e.message))</pre><p>FormStore usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efd1493ea4d2a9a6e053d691d4a7495d/href">https://medium.com/media/efd1493ea4d2a9a6e053d691d4a7495d/href</a></iframe><h4>Conclusion</h4><p>In this tutorial I explained how to handle React forms validation and keep it as <strong>simple</strong> as possible using MobX.</p><p>It’s <strong>scalable</strong>: you can add more methods to superclass — the FormStore, if needed, and override/add custom ones to the <strong>subclasses. </strong>And this solution is pretty simple, isn’t it?</p><h4>What is next?</h4><p>For now, it’s my approach to form validation of React components.</p><p>Features to consider:</p><ul><li>Show validation errors only when the user stopped typings (onBlur).</li><li>Delay validation errors until the user submits the form.</li></ul><p>So, in future I’ll extend a superclass with more functionality — so there might be a part II. Let me know if you are interested in it.</p><p>Full working example can be found here:</p><p><a href="https://github.com/mkozhukharenko/mobx-form-validation">mkozhukharenko/mobx-form-validation</a></p><p>Additional resources to read:</p><ul><li><a href="https://blog.risingstack.com/handling-react-forms-with-mobx-observables">Handling React Forms with Mobx Observables</a> — explains how to use MobX observables for handling react-forms.</li><li><a href="https://spin.atomicobject.com/2016/10/05/form-validation-react">Elegant Form Validation Using React</a> — is more about creating a custom validator and attaching it to React.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ce00233ae27" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>