Validation on fields using index position

时光怂恿深爱的人放手 提交于 2019-12-23 18:41:59

问题


I have a couple of fields that I would like to replicate dynamically. I'm using ng-reapt which is doing the job. However, the validation messages are not working. Here's what I've got:

<html ng-app="app">
    <head>
        <title>teste</title>
    </head>
    <body ng-controller='testController'>
        <label>Number of Workers</label>
        <select ng-model="quantity" ng-change="changed()">
            <option value="1" selected="selected">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>

        <form name='frm' action='/workers/add' method='POST'>
            <div ng-repeat="i in numberOfWorkers track by $index">{{$index + 1}}
                <div>
                    <label>First Name</label>
                    <input class="fullSize" letters-only placeholder="Please enter your name" type="text" name="firstName" ng-model="workers[$index].firstName" ng-minlength="3" ng-maxlength="50" ng-required="true" maxlength="50">
                    <span ng-cloak class="error-container" ng-show="submitted || showErrors(frm.firstName)">
                        <small class="error" ng-show="frm.firstName[{{$index}}].$error.required">* Please enter your name.</small>
                        <small class="error" ng-show="frm.firstName[{{$index}}].$error.minlength">* At least 3 chars.</small>
                        <small class="error" ng-show="frm.firstName[{{$index}}].$error.maxlength">* No more than 50 chars.</small>
                    </span>
                </div>
                <div>
                    <label>Surname</label>
                    <input class="fullSize" letters-only placeholder="Please enter your surname" type="text" name="surName" ng-model="workers[$index].surName" ng-minlength="3" ng-maxlength="50" ng-required="true" maxlength="50">
                    <span ng-cloak class="error-container" ng-show="submitted || showErrors(frm.surName)">
                        <small class="error" ng-show="frm.surName[$index].$error.required">* Please enter your name.</small>
                        <small class="error" ng-show="frm.surName[$index].$error.minlength">* At least 3 chars.</small>
                        <small class="error" ng-show="frm.surName[$index].$error.maxlength">* No more than 50 chars.</small>
                    </span>                 
                </div>
                <div>
                    <label>Email</label>
                    <input class="grid-full" placeholder="Please enter your e-mail" type="email" name="email" ng-model="workers[$index].email" ng-minlength="3" ng-maxlength="50" required maxlength="50">
                    <span class="error-container" ng-show="submitted || showErrors(frm.email)">
                        <small class="error" ng-show="frm.email[$index].$error.required">* Please enter your E-mail.</small>
                        <small class="error" ng-show="frm.email[$index].$error.email">* Invalid email.</small>
                    </span>
                </div>

            </div>
        </form>

        <button ng-click="test()">test</button>

        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
        <script>
            var app = angular.module("app",[]);
            app.controller('testController',['$scope', function($scope){

                $scope.quantity = 1;
                $scope.submitted = false;
                $scope.numberOfWorkers = [1];
                $scope.workers = [];

                $scope.getNumber = function (num) {
                    return new Array(num);
                };

                $scope.test = function(){

                    console.log($scope.workers);
                };

                $scope.changed = function() {
                  $scope.workers = [];
                  $scope.numberOfWorkers = new Array(parseInt($scope.quantity));
                }


                $scope.isUndefined = function (thing) {
                    var thingIsUndefined = (typeof thing === "undefined");

                    return thingIsUndefined;
                };

                $scope.showErrors = function (field) {
                    var fieldIsUndefined = $scope.isUndefined(field);

                    if (fieldIsUndefined == false)
                    {
                        var stateInvalidIsUndefined = $scope.isUndefined(field.$invalid);
                        var stateDirtyIsUndefined = $scope.isUndefined(field.$dirty);

                        return (fieldIsUndefined == false && stateInvalidIsUndefined == false && stateDirtyIsUndefined == false &&
                            (field.$invalid && field.$dirty));
                    }
                    return false;
                };

            }]);
        </script>
    </body>
</html>

Is there a way to display and validate fields using the index position?

frm.firstName.$error.minlength

The code above works for the first block, but it display the message to all copies.


回答1:


I think this along the lines of what you're trying to achieve:

http://plnkr.co/edit/HVwnRfvt30WrsteY6DvW

We need to assign a dynamic name to each input field, by way of using the $index of the ngRepeat:

<form name="form">
    <div ng-repeat="entry in array track by $index">
        <input type="text" name="someField{{$index}}">
    <div>
</form>

And based on the number of entries in array, we can access in a loop each dynamically created someField using the syntax below:

form['someField' + $index]

In your case, let's say we have three workers, and the second worker's firstName is invalid -- in an ngRepeat, it would look like this:

  <div ng-repeat="i in numberOfWorkers track by $index">
    isValid? --> form.firstName{{$index}}.$valid = {{form['firstName' + $index].$valid}}
  </div>

Output:

isValid? --> form.firstName0.$valid = true
isValid? --> form.firstName1.$valid = false
isValid? --> form.firstName2.$valid = true


来源:https://stackoverflow.com/questions/33974247/validation-on-fields-using-index-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!