<?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 Raul Lukacs on Medium]]></title>
        <description><![CDATA[Stories by Raul Lukacs on Medium]]></description>
        <link>https://medium.com/@raullukacs?source=rss-4b6b8d977998------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*JOS4hLmIaEE6WjTd.</url>
            <title>Stories by Raul Lukacs on Medium</title>
            <link>https://medium.com/@raullukacs?source=rss-4b6b8d977998------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 17:14:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raullukacs/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[Forms in Angular]]></title>
            <link>https://medium.com/@raullukacs/how-to-work-with-forms-in-angular-a5236af55f8f?source=rss-4b6b8d977998------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5236af55f8f</guid>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[angular-6]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[forms-design]]></category>
            <dc:creator><![CDATA[Raul Lukacs]]></dc:creator>
            <pubDate>Tue, 04 Sep 2018 12:47:53 GMT</pubDate>
            <atom:updated>2018-09-05T14:04:24.216Z</atom:updated>
            <content:encoded><![CDATA[<p>Angular provides 2 approaches for dealing with forms in an application: <a href="https://angular.io/guide/reactive-forms">reactive forms</a> and <a href="https://angular.io/guide/forms#template-driven-forms">template-driven</a> forms. While template-driven forms are simpler to use and are similar with the AngularJs form implementation, <strong>reactive forms</strong> are more <strong>flexible</strong>, can <strong>handle</strong> any <strong>complex scenarios, easier to unit test </strong>and they allow <strong>more component code</strong> and less HTML markup. (for more information about these always trust the <a href="https://angular.io/guide/forms">official documentation</a>)</p><p>In this post we are going to cover the following scenarios:</p><ol><li>text input</li><li>select input</li><li>radio group</li><li>checkbox</li><li>multiple checkbox selection</li></ol><h3>Now let’s get coding !</h3><h3>Text input</h3><p>We are going to start easy and create the form with the basic text input and build on it.</p><p>First let’s write the component code.We will validate the field to be required and valid using a regex pattern.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*MdzPtcWcQFX4jeaLeO71QA.jpeg" /></figure><p>Now let’s add the html markup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/1*S51VBXTIDhAv4728_snhCg.jpeg" /></figure><p>Yay! It’s working.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*5fcFKHURgnRZnO_QsqsCFg.gif" /><figcaption>Working example</figcaption></figure><h3>Select input</h3><p>Now let’s add select input that we might use when we have dropdown selection in the form. We will validate this select input to be required.</p><p>In the component we defined an array of { id, label } objects called <strong>selectOptions </strong>that will be used in the html markup for rendering the available options.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*UZEfVObN5DtXx5CuH2m5qw.jpeg" /></figure><p>Let’s update the markup and render the select by using the array of options we defined in the component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*_11sN3O9Qxg7aNCId7nppw.jpeg" /><figcaption>Added the select input to the existing form</figcaption></figure><p>Yay! Still working.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*NZZ-dmDLXTdvedgEl2wtMg.gif" /><figcaption>Working example</figcaption></figure><h3>Radio group</h3><p>Next we are going to add a radio group. Let’s use sexes for our example: male, female options.</p><p>Again we need to update our component code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*3rEFvQSGJHsUr9SifLX2wA.jpeg" /><figcaption>Add new field and static options</figcaption></figure><p>Now the html markup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*-kG8J4F_j1RK-OvcSaFXjA.jpeg" /><figcaption>Added new radio group markup on the existing form</figcaption></figure><p>Let’s see it in action!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*LOOOf7jwpoJ_kJjdeqmSxw.gif" /></figure><h3>Checkbox</h3><p>While building an app we definitely will have an ”I agree with the terms and conditions” checkbox and here is how we can build it.</p><p>We first add the field in the forms definition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*d8jYkziH3vnW_SsgK2og_Q.jpeg" /></figure><p>Then we need to update the html markup</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/844/1*-EwQE0FTDf5cu8Des_dJEA.jpeg" /><figcaption>Add checkbox to existing form</figcaption></figure><p>And taa daa!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*Bfib0WBci65S6RqqN6cfSA.gif" /></figure><h3>Multiple checkbox selection</h3><p>Lastly we are going to build a multiple checkbox selection form. For this we will need to use array function for the form builder.</p><p>For these example we need to create a custom validation function that will return if at least one options was selected or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*UI5oLVVEh4dOp4guJHOLqg.jpeg" /></figure><p>Let’s see how we need to build the markup:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*S0ttD1UUpB2VAhnL3di_hg.jpeg" /><figcaption>Added the new field in the existing form</figcaption></figure><p>Great! Works like a charm!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*yP9WcBb6mjDMKZO1A72MaQ.gif" /><figcaption>Full Working Example</figcaption></figure><p>The entire demo application code source can be accessed on <a href="https://github.com/raullukacs/angularforms">github</a>.</p><p><strong>Thanks for reading! Hope it helped! </strong>And remember: when in doubt always check the <a href="https://angular.io/docs">angular official documentation</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5236af55f8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>