I am trying to rebuild a form with several file selectors with primeng but primeng does only offer a FileUpload component which is not the same as a file selector. Same seems to apply for ngx-dropzone.
Example with one file selector:
<div>
<form [formGroup]="model" (ngSubmit)="onSubmit()">
<label for="fileUpload">Csv-File:</label>
<input
id="fileUpload"
type="file"
formControlName="csvFile"
/>
<p>Complete the form to enable button.</p>
<button type="submit" [disabled]="!model.valid">Submit</button>
</form>
</div>
with
import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-input',
templateUrl: './input.component.html',
styleUrls: ['./input.component.scss'],
})
export class InputComponent {
model = new FormGroup({
csvFile: new FormControl('', [Validators.required]),
});
onSubmit() {
console.log(JSON.stringify(this.model.value));
}
}
Example which swagger would create from a spring boot app.
What I have done so far is the following. However, that seems not the the best way to do that.
<div>
<form (ngSubmit)="onSubmit()" [formGroup]="form">
<div class="row" id="csvFileUpload">
<div class="col">
<p-button icon="pi pi-file" label="csv-Datei auswählen">
<input
(input)="onCsvFileSelected($event)"
formControlName="csvFile"
style="opacity: 0; position: absolute; width: 100%; height: 100%"
type="file"
/>
</p-button>
</div>
<div class="col left-spacer">{{ csvFileShadowed.name }}</div>
</div>
<p>Complete the form to enable button.</p>
<p-button [disabled]="!form.valid" type="submit">Submit</p-button>
</form>
</div>
import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-koko',
templateUrl: './koko.component.html',
styleUrls: ['./koko.component.scss']
})
export class KokoComponent {
csvFileShadowed = {} as File;
form = new FormGroup({
csvFile: new FormControl('', [Validators.required])
});
onSubmit() {
console.log(this.csvFileShadowed);
}
onCsvFileSelected($event: Event) {
if ($event.target && $event.target instanceof HTMLInputElement) {
let inputEvent = $event.target as HTMLInputElement;
if (inputEvent.files?.length == 1) {
this.csvFileShadowed = inputEvent.files[0];
}
}
}
}
.row {
display: flex; /* equal height of the children */
width: fit-content;
}
.col {
flex: 1; /* additionally, equal width */
margin: 0;
padding: 0;
display: flex;
align-items: center;
}
.left-spacer {
margin-left: 0.5em;
max-width: 50%;
}
p-button {
white-space: nowrap;
}
- What would be the reason that primeng does not offer a file selector component?
- What would be the best way to use several file selectors in a (reactive) form?