0

I cannot figure out how can I count form fields which are valid:

For template i'm using common code:

     <p class="input-group" ng-repeat="pomiar in pomiary track by $index">

        <input type="number" class="form-control" ng-model="pomiar" is-float/>
        <span class="input-group-btn">
          <input class="btn btn-danger" ng-click="usunPomiar($index)"
          type="button" value="X">
        </span>
      </p>

Validation is done via directive :

   .directive('isFloat', function ($filter) {

var FLOAT_REGEXP_3 = /^\$?\d+(\.\d*)?$/; //Numbers like: 1123.56
var FLOAT_REGEXP_4 = /^\$?\d+(\,\d*)?$/; //Numbers like: 1123,56

return {
    require: 'ngModel',
    link: function (scope, elm, attrs, ctrl) {
        ctrl.$parsers.unshift(function (viewValue) {
          if (FLOAT_REGEXP_3.test(viewValue)) {
                    ctrl.$setValidity('float', true);
                    return parseFloat(viewValue);
            } else if (FLOAT_REGEXP_4.test(viewValue)) {
                    ctrl.$setValidity('float', true);
                    return parseFloat(viewValue.replace(',', '.'));
            }else {
                ctrl.$setValidity('float', false);
                return undefined;
            }
        });

        ctrl.$formatters.unshift(
           function (modelValue) {
               return $filter('number')(parseFloat(modelValue) , 2);
           }
       );
    }
};
  });

I would like to display nuber of fields(inputs) in the template or store it in variable.

No problem if input isn't array and name attribute 'pomiar' is set:

     {{myForm.pomiar.$valid}}

Do I have to write function in controller to validate it once again, or is there simpler way?

Jan Pi
  • 167
  • 2
  • 13
  • can't set `ng-model` to `pomiar` ... use `pomiary[$index]`. Inspect the form object created by form name for more details where you can iterate to check valid inputs – charlietfl Sep 16 '15 at 21:06
  • last block of code is just example, sorry, it might be disorienting... Looks like I also can run into this: [link](http://stackoverflow.com/questions/12977894/what-is-the-angularjs-way-to-databind-many-inputs) – Jan Pi Sep 16 '15 at 21:43

0 Answers0