Making AngularJS directive for <input type=“file”> validation

Im trying to make a directive able to handle an <input type="file"> validation inside a <form> given that AngularJS doesn’t have support for this…It kind of works to check if a file is selected, but I also have a <textarea> in the form so when I select a file the form gets state $valid=true, but just by typing into the <textarea> makes the form become $valid=false even though I haven’t set a validation for the <textarea> . Why does this happen? How can I fix it?. Here is a simplified example designed to see how the program fails in order to illustrate the problem:

My app.js file:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $ = 'World';

app.directive('validFile', function () {
    return {
      restrict: "A",
      require: '^form',

      link: function (scope,elem,attrs, ctrl) {

        elem.bind("change", function(e) {


My index.html file:

    <!doctype html>
<html ng-app="plunker" >
  <meta charset="utf-8">
  <title>AngularJS Plunker</title>
  <script>document.write('<base href="' + document.location + '" />');</script>
  <link rel="stylesheet" href="style.css">
  <script src=""></script>
  <script src="app.js"></script>
<body ng-controller="MainCtrl">
  <div ng-form="myForm" >
    <input ng-model="filename" valid-file required type="file"/>
    <button ng-disabled="myForm.$invalid" type="submit" class="btn btn-primary"><i class="icon-white icon-ok"></i>&nbsp;Ok</button>
    <div >
      <textarea name="observations" rows="3" cols="50" ng-model="observations"></textarea>
      Input is valid: {{myForm.$valid}} Input is invalid: {{myForm.$invalid}}
      <br>Selected file: {{filename}}
      <br>Area is valid: {{myForm.observations.$valid}} Area is invalid: {{myForm.observations.$invalid}}

And a working plnkr to see the hole application, intentionally I’ve made the code so you can see the form and the textarea valid state and how a change on the textarea make the form invalid but the textarea itself doesn’t:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.