<?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>DEV Community: Justin Thadathil</title>
    <description>The latest articles on DEV Community by Justin Thadathil (@justinthadathil).</description>
    <link>https://dev.to/justinthadathil</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%2F826244%2F94da98e0-2305-453f-ab16-1281ffe9fb83.jpeg</url>
      <title>DEV Community: Justin Thadathil</title>
      <link>https://dev.to/justinthadathil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/justinthadathil"/>
    <language>en</language>
    <item>
      <title>Reactive FormArray Example in Angular</title>
      <dc:creator>Justin Thadathil</dc:creator>
      <pubDate>Sat, 26 Mar 2022 07:39:47 +0000</pubDate>
      <link>https://dev.to/justinthadathil/formarray-in-angular-1clo</link>
      <guid>https://dev.to/justinthadathil/formarray-in-angular-1clo</guid>
      <description>&lt;p&gt;In this article, I’d like to showcase the process to create and apply validations in &lt;strong&gt;FormArray&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What’s a FormArray&lt;/strong&gt;&lt;br&gt;
A FormArray is responsible for managing a collection of AbstractControl, which can be a FormGroup, a FormControl, or another FormArray. FormArray is one of the three fundamental building blocks used to define forms in Angular, along with FormControl and FormGroup.&lt;/p&gt;

&lt;p&gt;Note: for dropdown i have used Angular Syncfusion. please install its package and import the package. you can use any other package also&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;let Further see the steps to create a form array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;a. Import FormsModule and ReactiveFormsModule in your app.module.ts file.&lt;/p&gt;

&lt;p&gt;b. Now in the component file add FormGroup variable like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addMemberItemInputForm = {} as FormGroup;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c. call FormBuilder in the constructor like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private formBuilder: FormBuilder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;d. Now initialise form controls by calling it in a function or the constructor like i have used a function called createAddMemberItemInputForm. add the required validations to the controls. here &lt;strong&gt;itemDetails&lt;/strong&gt; is declared a form Array in the below code and i have added a common Validation for form array in which only 5 rows can be added. you can remove or change it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;createAddMemberItemInputForm(){
  this.addMemberItemInputForm = this.formBuilder.group({
     memberName: ['', Validators.required],
     itemType: ['', Validators.required],
     itemDetails: this.formBuilder.array(
    [this.createEmpFormGroup()],
    [Validators.required, Validators.maxLength(5)])
   });
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;e. Now i will use getter to binds an object property to a function in the &lt;code&gt;createAddMemberItemInputForm()&lt;/code&gt; memberName object property is bind to a function called &lt;code&gt;get memberName()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; get memberName() {
    return this.addMemberItemInputForm.get('memberName');
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;f. Like this for every control create a getter function.&lt;/p&gt;

&lt;p&gt;g. Now in the form array there is a function called &lt;code&gt;this.createEmpFormGroup()&lt;/code&gt; that is used to initialise the controls in form array like the code bellow&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;createEmpFormGroup(){
   return this.formBuilder.group({
     itemName: ['', [Validators.required]],
     quantity: ['', [Validators.required, Validators.max(5)]],
     price: ['', [Validators.required]]
   })
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;h. Now create a add and delete row for form array controls like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addEmployee() {
  let newMem = this.createEmpFormGroup();
  this.itemDetails.push(newMem);
}

deleteEmployee(i: number) {
  this.itemDetails.removeAt(i);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;i. Coming to the HTML side add the formGroup and formControlName&lt;/p&gt;

&lt;p&gt;j. For outer form control add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="col-6"&amp;gt;
   &amp;lt;label class="form-label"&amp;gt;Enter Member Name&amp;lt;/label&amp;gt;
   &amp;lt;input type="text" class="form-control" placeholder="Member Name" formControlName="memberName"&amp;gt;
  &amp;lt;label *ngIf="memberName?.invalid &amp;amp;&amp;amp; isValidFormSubmitted != null &amp;amp;&amp;amp; !isValidFormSubmitted" class="error"&amp;gt;
   Team name is required.
  &amp;lt;/label&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;k. For form Array add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;tbody formArrayName="itemDetails"&amp;gt;
  &amp;lt;tr  *ngFor="let item of itemDetails.controls; let i = index" [formGroupName]="i"&amp;gt;
   &amp;lt;td&amp;gt;
    &amp;lt;input type="text" class="form-control" placeholder="Enter Item Name" formControlName="itemName"&amp;gt;
    &amp;lt;label *ngIf="itemDetails.controls[i].get('itemName')?.errors?.['required'] &amp;amp;&amp;amp; isValidFormSubmitted != null &amp;amp;&amp;amp; !isValidFormSubmitted" class="error"&amp;gt;
     Employee name required.
  &amp;lt;/label&amp;gt;
 &amp;lt;/td&amp;gt;
&amp;lt;td&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;l. Now reaching this step you have almost implemented form array with proper validations.&lt;/p&gt;

&lt;p&gt;m. Entire code with live preview is on Github you can refer it.&lt;/p&gt;

&lt;p&gt;Code available on GitHub:- [(&lt;a href="https://github.com/justinthadathil/AngularFormArray"&gt;https://github.com/justinthadathil/AngularFormArray&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;Live preview on - [(&lt;a href="https://justinthadathil.github.io/Angular_Form_Array/"&gt;https://justinthadathil.github.io/Angular_Form_Array/&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>angularformarray</category>
      <category>formarray</category>
      <category>angular</category>
    </item>
    <item>
      <title>Upload images to Cloudinary using Angular</title>
      <dc:creator>Justin Thadathil</dc:creator>
      <pubDate>Fri, 18 Mar 2022 08:11:21 +0000</pubDate>
      <link>https://dev.to/justinthadathil/upload-images-to-cloudinary-using-angular-6oa</link>
      <guid>https://dev.to/justinthadathil/upload-images-to-cloudinary-using-angular-6oa</guid>
      <description>&lt;p&gt;Cloudinary is a SaaS based platform that helps you to store high quality images, where in your free tire you can store 25GB of images and videos. it has may other options to customize the image. &lt;br&gt;
     here I required the basic feature that is upload a image to Cloudinary via Angular. in my recent project, I was using firebase storage to store images but this time for a change thought to learn something new taking an inspiration by my tech enthusiast friend I though to try it out.&lt;/p&gt;

&lt;p&gt;I am using Angular version 12. here for this basic upload feature there is &lt;strong&gt;no need to import any Cloudinary package&lt;/strong&gt;. let's further roll out into the steps:- &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First lets set up Cloudinary part&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sign in or login to your Cloudinary account.&lt;/li&gt;
&lt;li&gt;now click on settings and then &lt;strong&gt;upload&lt;/strong&gt; tab.&lt;/li&gt;
&lt;li&gt;in the tab find for Upload presets option&lt;/li&gt;
&lt;li&gt;now click on &lt;strong&gt;Add upload preset&lt;/strong&gt; and add a Upload preset name.&lt;/li&gt;
&lt;li&gt;select Signing Mode as unsigned and add a folder name.&lt;/li&gt;
&lt;li&gt;now click on save button&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Now in the Angular Project&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From the input type file, get the file value&lt;/li&gt;
&lt;li&gt;Pass it to a function that accept the image data
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function onChangeImg(){
   data.append('file', file);
   data.append('upload_preset', 'Upload presets name here');
   data.append('cloud_name', 'copy from dashboard')
   data.append('public_id', file Name+todaysDate)
   this.myService.uploadSignature(data).subscribe((imageData) =&amp;gt; {
     this.imageUrl = imageData.url;
     this.registrationForm.patchValue({
       signatureUrl: imageData.url
     });
   })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now in the Service side
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//upload signature
  uploadSignature(vals): Observable&amp;lt;any&amp;gt;{
    let data = vals;
    return this.http.post('https://api.cloudinary.com/v1_1/cloud_name/image/upload',data)
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the angular once the function is submitted, then in the response you will get a image URL that can be store and used to access the uploaded image. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This were the few process to be followed which in return helps tp store image and that can be retrieved properly. Cloudinary is a good tool to fulfill the above requirements.&lt;/p&gt;

&lt;p&gt;Anyone reading the article faces any issues can comment bellow. I will love to reply. Thank you &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>typescript</category>
      <category>angular</category>
      <category>cloudinary</category>
    </item>
  </channel>
</rss>
