I have the code below:
import { Component, OnInit, ElementRef } from '@angular/core';
import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
showSort: boolean = false;
form: FormGroup;
name: FormControl;
sortSelf: FormControl;
sortItem: FormArray;
locationItems: FormArray;
constructor(private _fb: FormBuilder, private _elementRef: ElementRef ) {
}
ngOnInit(): void {
this.sortItem = this._fb.array([this.initSort()]);
this.form = this._fb.group({
sortItem: this.sortItem
});
}
initSort() {
return this._fb.group({
locationPicture: new FormControl('', []),
locationItems: this._fb.array([
this.initSortItems()
])
})
}
initSortItems() {
return this._fb.group({
itemPicture: new FormControl('', [])
})
}
addSort() {
this.sortItem.push(this.initSort());
}
addSortLocationItem(i?: number, t?: number) {
const control: FormArray = <FormArray> this.sortItem.at(i).get('locationItems');
control.push(this.initSortItems());
}
showImage(event, level?: string, i?: number, t?: number){
let file = event.target.files[0];
if(level === 'locationPicture'){
(<FormArray>this.sortItem.at(i)).controls['locationPicture'].setValue(file, {onlySelf: true});
}else{
(<FormArray>this.sortItem.at(i).get('locationItems')).at(t).get('itemPicture').setValue(file, {onlySelf: true});
}
}
next(value, valid: boolean){
console.log(`this is value ${JSON.stringify(value, null, 4)}`);
}
}
Mark Up:
<div class="col-md-8 col-md-offset-2">
<form class="" action="index.html" method="post" [formGroup]="form" (ngSubmit)="next(form.value, form.valid)">
<div class="form-group">
<div formArrayName="sortItem" class="">
<div class="top-buffer-small">
<a (click)="addSort()" style="cursor: default">
Add Sort Location +
</a>
</div>
<div *ngFor="let sortLocation of sortItem.controls; let i=index">
<div [formGroupName]="i" class="">
<div class="form-group">
<label>Location Picture</label>
<input type="file" formControlName="locationPicture" class="form-control locationPicture" accept="image/*" capture="camera" (change)="showImage($event, 'locationPicture', i, t)">
<!-- <input type="file" formControlName="locationPicture" (change)="showImage($event)" /> -->
<img src="" alt="" class="location-image" width="80" height="80">
<small class="form-text text-muted" [hidden]="sortItem.controls[i].controls.locationPicture.valid">
Picture is required
</small>
</div>
<div formArrayName="locationItems" class="col-md-10 col-md-offset-1">
<div class="row">
<a (click)="addSortLocationItem(i,t)" style="cursor: default">
Add Item +
</a>
</div>
<!-- <div class="from-group" *ngFor="let eachItem of form.controls.sortItem.controls[i].controls.locationItems.controls; let t=index"> -->
<div class="form-group" *ngFor="let eachItem of sortLocation.get('locationItems').controls; let t=index">
<div [formGroupName]="t">
<div class="form-group">
<label>Item Picture</label>
<input type="file" formControlName="itemPicture" class="form-control itemPicture" capture="camera" (change)="showImage($event, 'itemPicture', i, t)">
<img src="" alt="" class="item-image" width="80" height="80">
<small class="form-text text-muted" [hidden]="sortItem.controls[i].controls.locationItems.controls[t].controls.itemPicture.valid">
Picture is required
</small>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
<div class="text-center">
<button type="submit" name="button" class="btn btn-primary" [disabled]="form.invalid">Submit</button>
</div>
</form>
</div>
I am trying to update the form within the showImage method by updating the formcontrol values. However, I cannot get the form to update with the picture. When I try to upload it on the 'locationPicture' and 'itemPicture', I get the following error:
Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
Please any help with this will be great