Using reactive form validation with <input type=“file”> for an Angular app

前端 未结 3 1635
梦如初夏
梦如初夏 2020-12-30 03:46

I\'m writing a component with a file picker that uploads a file to our CDN. I\'m trying to add a reactive form on this component to validate the image input, so I can check

3条回答
  •  生来不讨喜
    2020-12-30 04:03

    As @ibrahim mention it's not implemented yet, but i got the same problem and solved it using hidden field. on onFileChange method set file.name to hidden field, where you can validate.

       

    {{file.name}}

    onFileChange($event) { let file = $event.target.files[0]; // <--- File Object for future use. this.form.controls['imageInput'].setValue(file ? file.name : ''); // <-- Set Value for Validation } fileName = ''; this.form = this.formBuilder.group({ imageInput: [fileName, Validators.required] });

提交回复
热议问题