I want to optimize my application so as not to use the same validation fields in two components. I use the fields for checking "title" and "url" in the component "GalleryAddComponent" (when I add an element) and the same fields for checking "title" and "url" in the component "GalleryItemComponent" (when I edit the component). In components, only the buttons in the form are different, in "GalleryAddComponent" there is a button "add" which calls the method of adding an element, and in the component "GalleryItemComponent" button "updatePost" which saves the changes in the element.
I understand that the fields need to be transferred to a separate component and connected in the forms of the components: "GalleryAddComponent" and "GalleryItemComponent". But how to do it correctly to save the validation of the buttons and their logic. Help please understand how to implement this idea and if you can set an example in "StackBlitz".
Link to the structure of my project: StackBlitz
template of GalleryAddComponent:
<h3>Add new product</h3>
<div class="card">
<div class="card-body">
<form [formGroup]="angForm" novalidate>
<div class="form-group">
<label class="col-md-4">Picture Title</label>
<input type="text" class="form-control" formControlName="title" minlength="1" #title/>
</div>
<div *ngIf="angForm.controls['title'].invalid && (angForm.controls['title'].dirty || angForm.controls['title'].touched)"
class="alert alert-danger">
<div *ngIf="angForm.controls['title'].errors.required">
Title is required.
</div>
</div>
<div class="form-group">
<label class="col-md-4">Picture Address (url)</label>
<input type="url" class="form-control" formControlName="url" #url pattern="https?://.+"
title="Include http://"/>
</div>
<div *ngIf="angForm.controls['url'].invalid && (angForm.controls['url'].dirty || angForm.controls['url'].touched)"
class="alert alert-danger">
Address(url) is required.
<div *ngIf="angForm.controls['url'].errors.required ">
</div>
</div>
<div class="form-group but-group">
<button (click)="addPost(title.value, url.value); angForm.reset(title.value, url.value)"
[disabled]="angForm.pristine || angForm.invalid"
class="btn btn-primary">Add
</button>
<a routerLink="/gallery" class="btn btn-danger">Back</a>
</div>
</form>
</div>
</div>
code of GalleryAddComponent:
export class GalleryAddComponent {
angForm: FormGroup;
isAdded: boolean = false;
constructor(private fb: FormBuilder, private galleryService: GalleryService) {
this.createForm();
}
createForm(): void {
this.angForm = this.fb.group({
title: ['', Validators.required],
url: ['', Validators.required]
});
}
addPost(title: string, url: string): void {
this.galleryService.add(title, url).subscribe(res => {
this.isAdded = true;
});
}
}
template of GalleryItemComponent:
<h4>Edit your post</h4>
<div class="card-body">
<form [formGroup]="angFormEd" novalidate>
<div class="form-group">
<label class="col-md-4">Picture Title</label>
<input type="text" class="form-control" formControlName="titleEd" #titleEd
/>
</div>
<div *ngIf="angFormEd.controls['titleEd'].invalid && (angFormEd.controls['titleEd'].dirty || angFormEd.controls['titleEd'].touched)"
class="alert alert-danger">
<div *ngIf="angFormEd.controls['titleEd'].errors.required">
Title is required.
</div>
</div>
<div class="form-group">
<label class="col-md-4">Picture Address (url)</label>
<input type="url" class="form-control" formControlName="urlEd" #urlEd pattern="https?://.+"
title="Include http://"/>
</div>
<div *ngIf="angFormEd.controls['urlEd'].invalid && (angFormEd.controls['urlEd'].dirty || angFormEd.controls['urlEd'].touched)"
class="alert alert-danger">
Address(url) is required.
<div *ngIf="angFormEd.controls['urlEd'].errors.required ">
</div>
</div>
<div class="form-group but-group">
<input type="button"
(click)="updatePost(titleEd.value, urlEd.value)"
[disabled]=" angFormEd.invalid"
class="btn btn-primary" value="Update Post">
</div>
</form>
</div>
code of GalleryItemComponent:
export class GalleryItemComponent implements OnInit {
pic: Picture;
angFormEd: FormGroup;
constructor( private route: ActivatedRoute, private galleryService: GalleryService, private fb: FormBuilder,) {
}
ngOnInit() {
this.createFormEd();
this.showPost();
}
createFormEd(): void {
this.angFormEd = this.fb.group({
titleEd: ['', Validators.required],
urlEd: ['', Validators.required]
});
}
showPost(): void {
this.route.data.subscribe(params => {
this.pic = params.post;
this.angFormEd.setValue({titleEd: params.post.title, urlEd: params.post.url});
})
}
updatePost(title: string, url: string): void {
this.route.params.subscribe(params => {
this.galleryService.update(title, url, params['id']).subscribe(res => {
if (res.id === this.pic.id) {
this.pic.title = title;
this.pic.url = url;
}
});
});
}
}
from How to use the same validation fields in two components?
No comments:
Post a Comment