Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a table with two column "user type" and "user Name" User type having a drop down selection So that i can select predefined user type and place a name in User name. There is a Add button on each click i am adding one another row.

I am doing one validation here So that user can't selected repeated "User type". for example from row 1 is i select "value 1" from drop down. then from row 2 i should not be able to select same value. Anyway this validation works fine. but there is one scenario if I add a duplicate row, then remove it right away, I am still unable to save because the validation error is still there.

here my jsp-

      <div ng-class="{'col-sm-9'}" class="col-md-10">
        <table class="table table-bordered table-striped table-hover table-condensed">
            <thead>
                <th>user type</th>
                <th>user Name</th>
                <th></th>
            </thead>
            <tbody>
                <tr ng-repeat="object in edituserConfig track by $index" ng-form="jobfileForm">
                    <td><select class="form-control" ng-model="object.key" required ng-change="reValidateJob()"  name="jobFileKey" ng-init="object.form = jobfileForm">

                            <option style="display:none" value=""></option>
                            <option value="value1">value1t</option>
                            <option value="value2">value2</option>
                            <option value="value3">value3</option>
                            <option value="value4">value4</option>
                            <option value="value5">value5</option>
                    </select></td>
                    <td><input type="text" class="form-control" name="jobFileName" ng-model="object.value" required/></td>

                    <td >
                          <button class="btn btn-danger btn-sm" data-style="zoom-in" ng-click="edituserConfig.splice($index,1)" ng-form="jobfileForm" title="Delete">
                                 <span class="glyphicon glyphicon-remove"></span>
                          </button>
                   </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <button class="btn btn-primary btn-sm" ng-click="edituserConfig.push({'key':'','value':''})"    title="Add Value">
                            <span class="glyphicon glyphicon-plus"></span> Add Value
                        </button>
                    </td>
                </tr>
            </tbody>
        </table>
        </div>

My controller-

        $scope.reValidateJob = function(){

                angular.forEach($scope.edituserConfig, function(fieldMapO) {
                    var count = 0;
                    angular.forEach($scope.edituserConfig, function(fieldMapI) {
                        if(fieldMapO.key == fieldMapI.key){
                            count ++;
                        }
                    });
                    if(count >1){
                        fieldMapO.form.jobFileKey.$setValidity("duplicate",false);
                    }else{
                        fieldMapO.form.jobFileKey.$setValidity("duplicate",true);
                    }
                });
            };

Please advise me on delete button do i need to vaidate all the row again? any good solution?

share|improve this question

Well, I think following should work. If not, I'd request you to create a js-fiddle or plunker.

add reValidateJob to ng-click of the add button like below

<button class="btn btn-danger btn-sm" data-style="zoom-in" ng-click="edituserConfig.splice($index,1); reValidateJob();" ng-form="jobfileForm" title="Delete">
    <span class="glyphicon glyphicon-remove"></span>
</button> 

Hope this helps!

share|improve this answer
    
I tried to add reValidateJob() function on delete button. But it dint work out cos what actually it is doing here is it is comparing the currently selected row with aothers row and if validation fails then preventing to save the data. But here Since the value is deleting So it is not reflecting to other rows value. – JOGO Mar 8 '16 at 14:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.