Skip to content

Commit

Permalink
Angular Forms In Depth
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jan 22, 2021
1 parent 9f836b9 commit 2ee68bc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/app/address-form/address-form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@

<input matInput
placeholder="Address Line 1"
formControlName="addressLine1">
formControlName="addressLine1" (blur)="onTouched()">

</mat-form-field>

<mat-form-field>

<input matInput
placeholder="Address Line 2"
formControlName="addressLine2">
formControlName="addressLine2" (blur)="onTouched()">

</mat-form-field>

<mat-form-field>

<input matInput
placeholder="Zip Code"
formControlName="zipCode">
formControlName="zipCode" (blur)="onTouched()">

</mat-form-field>

<mat-form-field>

<input matInput
placeholder="City"
formControlName="city">
formControlName="city" (blur)="onTouched()">

</mat-form-field>

Expand Down
57 changes: 55 additions & 2 deletions src/app/address-form/address-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ import {noop, Subscription} from 'rxjs';
@Component({
selector: 'address-form',
templateUrl: './address-form.component.html',
styleUrls: ['./address-form.component.scss']
styleUrls: ['./address-form.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi:true,
useExisting: AddressFormComponent
}
]
})
export class AddressFormComponent {
export class AddressFormComponent implements ControlValueAccessor, OnDestroy {

@Input()
legend:string;

onTouched = () => {};

onChangeSub: Subscription;

form: FormGroup = this.fb.group({
addressLine1: [null, [Validators.required]],
addressLine2: [null, [Validators.required]],
Expand All @@ -28,9 +39,51 @@ export class AddressFormComponent {
});

constructor(private fb: FormBuilder) {

}

registerOnChange(onChange: any) {
this.onChangeSub = this.form.valueChanges.subscribe(onChange);
}

ngOnDestroy() {
this.onChangeSub.unsubscribe();
}


writeValue(value: any) {
if (value) {
this.form.setValue(value);
}
}

registerOnTouched(onTouched: any) {
this.onTouched = onTouched;
}

setDisabledState(disabled:boolean) {
if (disabled) {
this.form.disable();
}
else {
this.form.enable();
}

}

}















Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<div [formGroup]="form">

<address-form legend="Address"></address-form>
<address-form legend="Address" formControlName="address"></address-form>

<mat-form-field>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class CreateCourseStep1Component implements OnInit {
releasedAt: [new Date(), Validators.required],
category: ['BEGINNER', Validators.required],
downloadsAllowed: [false, Validators.requiredTrue],
longDescription: ['', [Validators.required, Validators.minLength(3)]]
longDescription: ['', [Validators.required, Validators.minLength(3)]],
address: [null, Validators.required]
});

courseCategories$ : Observable<CourseCategory[]>;
Expand Down

0 comments on commit 2ee68bc

Please sign in to comment.