Skip to content

Commit

Permalink
solve--add-formarray
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Pentarakis committed Sep 8, 2024
1 parent 41f78ba commit 2b32982
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
27 changes: 17 additions & 10 deletions src/app/book/book-new/book-new.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@ <h2>New</h2>
<input formControlName="subtitle" />
</label>

<label class="form-field">
<span>Author</span>
<input formControlName="author" />
@if (form.controls.author.dirty) {
@if (form.controls.author.hasError('required')) {
<small>Please insert an Author.</small>
} @else if (form.controls.author.hasError('invalidAuthor')) {
<small>Name must not contain digits</small>
}
<ng-container formArrayName="authors">
@for (author of authors.controls; track author; let index = $index) {
<label class="form-field">
<span>Author</span>
<input [formControlName]="index" />

@if (author.dirty) {
@if (author.hasError('required')) {
<small>Please insert an Author.</small>
} @else if (author.hasError('invalidAuthor')) {
<small>Name must not contain digits</small>
}
}
</label>
<button (click)="deleteAuthor(index)">Remove Author</button>
}
</label>
</ng-container>
<button type="button" (click)="addAuthor()">Author hinzufügen</button>

<label class="form-field">
<span>Abstract</span>
Expand Down
27 changes: 24 additions & 3 deletions src/app/book/book-new/book-new.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, inject } from '@angular/core';
import {
FormArray,
FormControl,
FormGroup,
NonNullableFormBuilder,
Expand All @@ -13,7 +14,7 @@ interface BookForm {
isbn: FormControl<string>;
title: FormControl<string>;
subtitle: FormControl<string>;
author: FormControl<string>;
authors: FormArray<FormControl<string>>;
abstract: FormControl<string>;
}

Expand All @@ -32,13 +33,33 @@ export class BookNewComponent {
isbn: ['', [Validators.required]],
title: ['', [Validators.required]],
subtitle: [''],
author: ['', [Validators.required, validAuthorName()]],
authors: this.formBuilder.array([
['', [Validators.required, validAuthorName()]]
]),
abstract: ['']
});

submit() {
this.bookApiService.create(this.form.getRawValue()).subscribe();
// We need to handle the formArray now for authors separately
// Unfortunately the backend doesn't handle multiple authors yet
const firstAuthor = this.form.getRawValue().authors[0] || 'n/a';
this.bookApiService
.create(this.form.getRawValue())
.create({ ...this.form.getRawValue(), author: firstAuthor })
.subscribe();
}

get authors(): FormArray {
return this.form.controls.authors;
}

deleteAuthor(authorIndex: number) {
this.authors.removeAt(authorIndex);
}

addAuthor() {
this.authors.push(
this.formBuilder.control('', [Validators.required, validAuthorName()])
);
}
}

0 comments on commit 2b32982

Please sign in to comment.