Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate <input type="file"> with Angular2?

Tags:

angular

ngControl with a value of new Control('', Validators.required) didn't work even when the file is valid.

(and actually, I found it difficult to validate radio buttons as well...)

like image 906
chitakuma Avatar asked Jan 21 '16 16:01

chitakuma


People also ask

How to validate file upload in Angular?

The uploader component validate the selected files extension and size using the allowedExtentions , minFileSize and maxFileSize properties. The files can be validated before uploading to the server and can be ignored on uploading.

How does angular validate file size?

Check File Size before Uploading it in Angular Uploader component. By using uploading event, you can get the file size before upload it to server. File object contains the file size in bytes only. You can convert the size to standard formats ( KB or MB ) using bytesToSize method.

What are form validators?

Form validation is a “technical process where a web-form checks if the information provided by a user is correct.” The form will either alert the user that they messed up and need to fix something to proceed, or the form will be validated and the user will be able to continue with their registration process.


1 Answers

Validators.required depends on the value of the field.

Input type file does not have a value, therefore is considered as undefined or null.

That is why it is invalid. Better write you own validation.

For custom file validation example refer

like image 149
mayur Avatar answered Sep 22 '22 05:09

mayur