Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2085
Abstract
fined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="12c4">Let’s discuss what we are doing here, step by step.</p><ol><li>We just created a simple reactive Form with two fields, <code>Country</code> and <code>County</code>.</li><li>We have a drop-down for <code>Country</code> and whenever the drop-down value is changed, we are calling a function which is <code>onCountryChange()</code> to update the validator(s) for <code>County</code>.</li><li>Just to verify if the <code>Required</code> validator is set, we are checking if <code>County</code> field has <code>required</code> error or not.</li><li>We simply initialized the reactive form with default validation.</li><li>In <code>onCountryChange()</code> method, we are checking the value of the country selected. If this value is <code>B</code> (just in my case), we are setting the <code>Required</code> validator for <code>County</code> with the <code>setValidators()</code> method.</li><li>And if <code>Country</code> is not <code>B</code>, we are just clearing the validators.</li></ol><p id="27bf">Here’s how it will look in the browser :</p><figure id="4e8d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*NzKFNzZjUxB1hFim.png"><figcaption></figcaption></figure><figure id="7469"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*AuNZNcjaa66GJxJj.png"><figcaption></figcaption></figure><p id="76d2">And that’s it. It’s that simple to add or remove validators in Angular reactive forms.</p><h1 id="42a9">Some Important Points to Remember</h1><p id="e771">1. <code>setValidators()</code> method removes all the previous/default validators from form control. For example, let’s suppose during form initialization, you set <code>maxLength</code> and <code>minLength</code> validators for <code>County</code>. But once <code>setValidators([Validators.required])</code> executes, it will remove <code>maxLength</code> and <code>minLength</code> from <code>County</code> and set the <code>Required</code> validator only.</p><p i
Options
d="febb">So if you want to have <code>maxLength</code> and <code>minLength</code><b> </b>as well, just do something like this :</p><div id="1390"><pre><span class="hljs-function"><span class="hljs-title">setValidators</span><span class="hljs-params">([Validators.required, Validators.maxLength(<span class="hljs-number">10</span>)</span></span>, Validators<span class="hljs-selector-class">.minLength</span>(<span class="hljs-number">5</span>)])</pre></div><p id="988e">2. Always use the <code>updateValueAndValidity()</code> method after updating (either add or remove) validators. Because all your changes related to control will be reflected only if you put this statement, i.e., <code>updateValueAndValidity()</code>.</p><p id="19f8">3. Unfortunately, Angular doesn’t provide a way to remove only a single validator from form control. It has a <code>clearValidators()</code> method, but that will remove all the validators.</p><p id="b29b">For this, we can do two things. We can either use <code>clearValidators()</code> to remove all validators and then use <code>setValidators()</code> to set needed validation, or we can directly use <code>setValidators()</code> with the needed validators only (without the validator you don’t want to have).</p><p id="efb0">This is it for this article. I hope this was helpful.</p><h1 id="e325">References</h1><p id="45e5">The final code is available on GitHub:</p><div id="8f98" class="link-block">
<a href="https://github.com/gouravkajal/dummyApp-dynamicFormValidation">
<div>
<div>
<h2>gouravkajal/dummyApp-dynamicFormValidation</h2>
<div><h3>This project was generated with Angular CLI version 11.0.2. Run ng serve for a dev server. Navigate to…</h3></div>
<div><p>github.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div>
</div>
</div>
</a>
</div><p id="c044">Thanks for reading!</p></article></body>