1

I have a view with some inputs in a table row. Is there a way to check if the whole model is valid without using form (can't have form in tr ) ?

<tr ng-controller="SomeCtrl">
    <td>
        <input type="text" ng-model="someModel.name" required="required" ng-minlength="3">
    </td>
    <td>
        <input type="text" ng-model="someModel.x" required="required" ng-minlength="3">
    </td>
    <td>
        <input type="text" ng-model="someModel.y" required="required" ng-minlength="3">
    </td>
    <td>
        <button ng-click="save(someModel)">Save</button>
    </td>
</tr>

In controller i want to have something like this

function ($rootScope, $scope, serive) {
    $scope.save = function (someModel) {
        if (someModel.$valid) {}
    };
}
shrutyzet
  • 529
  • 3
  • 11

2 Answers2

0

If you use a form and it has a name, it automatically can give you exactly what you required.

<form name="someForm">
    <tr ng-controller="SomeCtrl">
        <td>
            <input type="text" ng-model="someModel.name" data-ng-required="true" ng-minlength="3">
        </td>
        <td>
            <input type="text" ng-model="someModel.x" data-ng-required="true" ng-minlength="3">
        </td>
        <td>
            <input type="text" ng-model="someModel.y" data-ng-required="true" ng-minlength="3">
        </td>
        <td>
            <button data-ng-disabled="!someForm.$valid" ng-click="someForm.$valid && Namesave(someModel)">Save</button>
        </td>
    </tr>
</form>

Otherwise, there is no automagical way to do that. I guess you can write a directive which gives you all your inputs and their validators, parse them, and have a validation on the whole model, but no such thing readily exists, I believe.

Nikola Yovchev
  • 9,498
  • 4
  • 46
  • 72
0

Better way to do it:

  • ng-form name="myForm" can be used to avoid using tag and it should work - plunker.
  • but using ng-include will make the form unavailable to controller - plunker , explanation. A work around for this issue is to pass the form as parameter to the controller method - plunker
Community
  • 1
  • 1
shrutyzet
  • 529
  • 3
  • 11