Free AI web copilot to create summaries, insights and extended knowledge, download it at here
3280
Abstract
les created by <code>create-vue@latest</code> inside <code>src/components/</code> and <code>src/assets/</code> directories are not needed in the context of this guide and can be removed.</i></p><h1 id="3166">Basic Form</h1><p id="bb62">For demonstration purposes we replace the content of the <code>App.vue</code> component with the following content:</p>
<figure id="6163">
<div>
<div>
<iframe class="gist-iframe" src="/gist/robinkloeckner/8538a3528039fc08955b22fc8c6e3ed4.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="4139">The <code><template></code> contains a form with two input fields, two fields for the error messages and a button component for submitting the form. Instead of using HTML5 <code><input></code> elements for the input fields <code>firstName</code> and <code>lastName</code>, we are using <code><Field /></code> components from VeeValidate. Both, will be rendered as text input fields as specified by the <code>type </code>attribute.</p><p id="331f">The rules defined with the <code>:rules</code> attribute are used by VeeValidate to validate the input. In the example above we only check, if the field holds any value (line 9 to 11). If not, we return an error message. The error message is then displayed in the corresponding <code><ErrorMessage /></code> component. In order to work correctly, the values of the <code>name</code> attributes of the <code><Field /></code> and the corresponding <code><ErrorMessage /></code> component must be the same.</p><p id="3d8b">Instead of using a HTML5 <code><button></code> element to submit the form, we use a <code><CustomButton /></code> component, which will be created and explained in the next step.</p><p id="1a1e">The elements of the form are wrapped with the VeeValidate <code><Form /></code> component, rather than the standard HTML5 <code><form></code> element. On submit, VeeValidate validates the input fields of the form according to the defined rules, before calling the <code>onSubmit()</code> function handler function (lines 5 to 7) as defined in the starting tag (line 15). The handler function simply logs the form values in the console.</p><h1 id="07f0">Submit Button</h1><p id="fabe">In <code>src/components/</code> create a new file <code>CustomButton.vue</code> with the following content:</p>
<figure id="28d8">
<div>
<div>
<iframe class="gist-iframe" src="/gist/robinkloeckner/8538a3528039fc08955b22fc8c6e3ed4.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="541a">The template contains a <code><button></code> element, whose <code>:disabled</code> attribute is bound to the <code>useIsFormValid()</code> and <code>useIsformDirty()</code> helper functions from VeeValidate via the computed property <code>isDisabled</code> (line 8 to 9). Both helper functions return computed refs to the values of the <code>valid</code> and <code>dirty</cod
Options
e> properties from the form’s <code>meta</code> state, which can be retrieved with <code>valid.value</code> and <code>dirty.value</code> respectively. <code>valid</code> becomes <code>true</code>, when the form has been successfully validated and <code>false</code> otherwise. <code>dirty</code> becomes true if the value of at least one field has been changed. Therefore, <code>isDisabled</code> only returns <code>false</code> if both values <code>isDirty</code> and <code>isValid</code> are <code>true</code>. At this point the form is completely filled and validated and the submit button can be enabled.</p><p id="23b4">The reason for using the <code>valid</code> and the <code>dirty</code> property rather than just <code>valid</code> is that <code>valid</code> could be <code>true</code> in situations where the form has not yet been validated.</p><p id="0a5c">From the <a href="https://vee-validate.logaretm.com/v4/api/composition-helpers#api-reference">documentation</a> on <code>useIsFormValid()</code>:</p><blockquote id="0e49"><p>Creating disabled buttons based on the <code>valid</code> attribute isn't accurate, because if the form hasn't been validated yet it, the <code>valid</code> property will be <code>true</code> which isn't accurate. You should combine <code>valid</code> checks with <code>dirty</code> state to get the most accuracy.</p></blockquote><p id="f4b2">Another thing to point out is that the <code>useIsFormValid()</code> and <code>useIsFormDirty()</code> helper functions are available to a <code><Form /></code> component’s child components. That is why the submit button is not implemented in <code>App.vue</code> but in a separate component which in turn is intended to be used as a child of the <code><Form /></code> component.</p><p id="9850">From the <a href="https://vee-validate.logaretm.com/v4/api/composition-helpers">documentation</a>:</p><blockquote id="f1ff"><p>These functions expose validation state to child components, most of these functions expose 2 variants of each state. On a form level and a field level.</p></blockquote><h1 id="ce55">Result</h1><p id="01ac">Rebuild the project with</p><div id="3ae6"><pre>npm run dev</pre></div><p id="d5e6">and browse to<a href="http://127.0.0.1:5173."><code>http://localhost1:5</code>173.</a></p><figure id="c886"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*P484tP8HRyW0DMuiCvdLdQ.gif"><figcaption></figcaption></figure><p id="8e8f">As you can see, the submit button is disabled, as long as both input fields are invalid.</p><h1 id="e630">Summary</h1><p id="b313">When using VeeValidate 4 for form validation, the submit button can be disabled for invalid forms in different ways. In this guide, we used the VeeValidate <code>useIsFormDirty()</code> and <code>useIsFormValid()</code> helper functions to enable/disable the submit button. Since the helper functions expose the validation state to <code><Form /></code> child components, the submit button is implemented as a separate component, which in turn is used as child component of <code><Form /></code>.</p><h1 id="af00">Source Code</h1><p id="f887"><a href="https://gist.github.com/robinkloeckner/8538a3528039fc08955b22fc8c6e3ed4?file=CustomButton.vue">Gist</a></p></article></body>