diff --git a/forms/src/app/app.component.html b/forms/src/app/app.component.html index 266cad7..da84e31 100644 --- a/forms/src/app/app.component.html +++ b/forms/src/app/app.component.html @@ -1,8 +1,9 @@

{{title}}

- + + diff --git a/forms/src/app/app.module.ts b/forms/src/app/app.module.ts index 8f60312..d3079f2 100644 --- a/forms/src/app/app.module.ts +++ b/forms/src/app/app.module.ts @@ -4,16 +4,16 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { HttpModule } from '@angular/http'; import { AppComponent } from './app.component'; -import { LoginFormComponent } from './login-form/login-form.component'; import { ModelFormComponent } from './model-form/model-form.component'; import { LoginModelFormComponent } from './login-model-form/login-model-form.component'; +import { ArrayFormComponent } from './array-form/array-form.component'; @NgModule({ declarations: [ AppComponent, - LoginFormComponent, ModelFormComponent, - LoginModelFormComponent + LoginModelFormComponent, + ArrayFormComponent ], imports: [ BrowserModule, diff --git a/forms/src/app/array-form/array-form.component.css b/forms/src/app/array-form/array-form.component.css new file mode 100644 index 0000000..1b672dc --- /dev/null +++ b/forms/src/app/array-form/array-form.component.css @@ -0,0 +1,25 @@ +:host { + display: flex; +} + +.container { + background-color: #E2CEFF; +} + +button[disabled] { + color: gray; +} + +.output { + padding: 10px; + border-radius: 10px; + background: #f4e6ff; +} + +.error { + color: red; +} + +.notify { + color: red; +} diff --git a/forms/src/app/array-form/array-form.component.html b/forms/src/app/array-form/array-form.component.html new file mode 100644 index 0000000..22383aa --- /dev/null +++ b/forms/src/app/array-form/array-form.component.html @@ -0,0 +1,39 @@ +
+

New Client (Dynamic Fields with FormArrays)

+
+ +
+ + +

Name is required.

+ + + +

Address is required.

+
+
+ +
+ +
+
+ + + +
+
+ +
+
+
+
diff --git a/forms/src/app/array-form/array-form.component.spec.ts b/forms/src/app/array-form/array-form.component.spec.ts new file mode 100644 index 0000000..ee59d9e --- /dev/null +++ b/forms/src/app/array-form/array-form.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ArrayFormComponent } from './array-form.component'; + +describe('ArrayFormComponent', () => { + let component: ArrayFormComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ArrayFormComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ArrayFormComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/forms/src/app/array-form/array-form.component.ts b/forms/src/app/array-form/array-form.component.ts new file mode 100644 index 0000000..05baa5b --- /dev/null +++ b/forms/src/app/array-form/array-form.component.ts @@ -0,0 +1,41 @@ +import { Component, OnInit } from '@angular/core'; +import { FormArray, FormControl, Validators, FormBuilder, FormGroup } from '@angular/forms'; + +@Component({ + selector: 'array-form', + templateUrl: './array-form.component.html', + styleUrls: ['./array-form.component.css'] +}) +export class ArrayFormComponent { + form: FormGroup; + + get emailsControls(): FormArray { + return this.form.get('emails') as FormArray; + } + + set emailsControls(formArray: FormArray) { + this.form.setControl('emails', formArray); + } + + constructor(private formBuilder: FormBuilder) { + this.form = this.formBuilder.group({ + name: ['', Validators.required], + address: ['', Validators.required], + emails: this.formBuilder.array([]), + }); + } + + addEmail() { + this.emailsControls.push(new FormControl('')); + } + + save() { + console.log(this.form.value); + } + + reset() { + this.emailsControls = this.formBuilder.array([]); + this.form.reset(); + } + +} diff --git a/forms/src/app/login-form/login-form.component.css b/forms/src/app/login-form/login-form.component.css deleted file mode 100644 index bbff942..0000000 --- a/forms/src/app/login-form/login-form.component.css +++ /dev/null @@ -1,11 +0,0 @@ -:host { - display: flex; -} - -.container { - background-color: #E2CEFF; -} - -button[disabled] { - color: gray; -} diff --git a/forms/src/app/login-form/login-form.component.html b/forms/src/app/login-form/login-form.component.html deleted file mode 100644 index d25fec2..0000000 --- a/forms/src/app/login-form/login-form.component.html +++ /dev/null @@ -1,32 +0,0 @@ -
-

Login (Template based)

-
-
- - -
- -
- - -
- -
- -
- - -
-
diff --git a/forms/src/app/login-form/login-form.component.spec.ts b/forms/src/app/login-form/login-form.component.spec.ts deleted file mode 100644 index 20a6416..0000000 --- a/forms/src/app/login-form/login-form.component.spec.ts +++ /dev/null @@ -1,28 +0,0 @@ -/* tslint:disable:no-unused-variable */ -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { By } from '@angular/platform-browser'; -import { DebugElement } from '@angular/core'; - -import { LoginFormComponent } from './login-form.component'; - -describe('LoginFormComponent', () => { - let component: LoginFormComponent; - let fixture: ComponentFixture; - - beforeEach(async(() => { - TestBed.configureTestingModule({ - declarations: [ LoginFormComponent ] - }) - .compileComponents(); - })); - - beforeEach(() => { - fixture = TestBed.createComponent(LoginFormComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); diff --git a/forms/src/app/login-form/login-form.component.ts b/forms/src/app/login-form/login-form.component.ts deleted file mode 100644 index 792f090..0000000 --- a/forms/src/app/login-form/login-form.component.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Component, OnInit } from '@angular/core'; - -@Component({ - selector: 'login-form', - templateUrl: './login-form.component.html', - styleUrls: ['./login-form.component.css'] -}) -export class LoginFormComponent implements OnInit { - - constructor() { } - - ngOnInit() { - } - - onSubmit(value) { - console.log(JSON.stringify(value)); - } - -} diff --git a/forms/src/app/login-model-form/login-model-form.component.html b/forms/src/app/login-model-form/login-model-form.component.html index 604179a..f464d0c 100644 --- a/forms/src/app/login-model-form/login-model-form.component.html +++ b/forms/src/app/login-model-form/login-model-form.component.html @@ -1,4 +1,4 @@ -
+

Login (Template based)

diff --git a/forms/src/app/model-form/model-form.component.html b/forms/src/app/model-form/model-form.component.html index 377ff4a..0bd6fc8 100644 --- a/forms/src/app/model-form/model-form.component.html +++ b/forms/src/app/model-form/model-form.component.html @@ -1,6 +1,6 @@ -
+

New Client (Model driven)

- +