Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i validate angular-multi-select?

I am using angular-multi-select from http://isteven.github.io/angular-multi-select/. It works according to my need. I am using multi-select in modal form and i want to validate it but it is not validating when i use required option. Can anyone suggest me how to validate angular-multi-select in form.

code

<div class="form-group" ng-class="{'has-error' :batch_form.end_time.required || batch_form.start_time.required || batch_form.days.$invalid && (!batch_form.days.$pristine || submitted) }">
    <label for="">Timings</label>
    <div ng-repeat="batch_timing in batch_timings" >
        <div class="row" ng-hide="batch_timing._destroy == 1">
             <div class="col-md-10">
                 <div class="row new-timings">
                    <div class="col-md-1">
                      <span class="text-style pull-left">Days</span>
                    </div>
                    <div class="timings-row" >
                      <div class="col-md-11">
                          {{batch_form.start_time.$error.required}}
                          <div 
                            multi-select
                            input-model="batch_timing.days"
                            output-model="resultData"
                            button-label="acronym"
                            item-label="acronym symbol name"
                            tick-property="ticked"
                            helper-elements="all none reset"
                            required name="days">
                          </div>  
                      </div>
                    </div>
                  </div>
                  <div class="row col-md-offset-1 new-timings">
                    <div class="col-md-1">
                      <span class="text-style">from</span>
                    </div>
                    <div class="col-md-4">
                      <select 
                        class="form-control"
                        data-ng-model="batch_timing.start_time" 
                        ng-options="timing as timing for timing in timings"
                        required name="start_time">
                      </select> 
                    </div>  
                    <div class="col-md-1">
                      <span class="text-style">to</span>
                    </div>
                    <div class="col-md-4">
                      <select class="form-control"
                            data-ng-model="batch_timing.end_time" 
                            ng-options="timing as timing for timing in timings"
                            required name="end_time">
                      </select>                         
                    </div> 

                  </div>
                </div>
                <div class="col-md-2">
                  <span class="delete-button">
                      <a ng-hide="batches.single.id" class="btn btn-danger" data-ng-model="batch_timing.id" href ng-click="batch_timings.splice($index, 1)"  ><i class="glyphicon glyphicon-trash"></i></a>
                      <a ng-show="batches.single.id" class="btn btn-danger" data-ng-model="batch_timing.id" href ng-click="remove(batch_timing.id, batch_timings)"  ><i class="glyphicon glyphicon-trash"></i></a>

                  </span>
                </div>      
              </div>   
            </div> 
            <div class="help-block col-lg-12 col-md-12 col-sm-12" ng-show="batch_form.days.$error.required && (!batch_form.start_date.$pristine || submitted)"> Start time and end time are required. 
            </div>
            <div class="help-block error" ng-show="batch_form.start_time.$error.required && batch_form.end_time.$error.required && (!batch_form.start_date.$pristine || submitted)"> Start time and end time are required. 
            </div>

            <div class="row">
              <div class="col-md-12">
                <a class="btn btn-primary" href ng-click="newTiming($event)"><i class="glyphicon glyphicon-plus"></i></a>
              </div>
            </div>  

   </div>

Thanks in advance

like image 943
Anusha Nilapu Avatar asked Dec 14 '22 20:12

Anusha Nilapu


2 Answers

I just used an invisible input field with the same output model. And set it to display: none.

<div class="col-md-11">
{{batch_form.start_time.$error.required}}
<div multi-select 
  input-model="batch_timing.days" 
  output-model="resultData" 
  button-label="acronym" 
  item-label="acronym symbol name" 
  tick-property="ticked" 
  helper-elements="all none reset">
</div>

<input type="text" ng-model="resultData" name="days" style="display:none" required />

But be careful with the name - just remove the name attribute from the multi-select and add it to your hidden input field.

like image 84
ilmgb Avatar answered Dec 28 '22 07:12

ilmgb


Expanding on ilmgb answer:

In case you also want to add a validation class to the control on validation error

<div multi-select 
  input-model="batch_timing.days" 
  output-model="resultData" 
  button-label="acronym" 
  item-label="acronym symbol name" 
  tick-property="ticked" 
  helper-elements="all none reset"
  class="resultDataSelect"
  >


<input type="text" ng-model="resultData" name="days" style="display:none" required />
<div ng-show="myForm.days.$error.required" class="validation-error">Days are required</div>

Add this watch in your controller to append the class when the output model changes:

$scope.$watch(function (scope) { return $scope.resultData },
           function (newValue, oldValue) {

               if (newValue.length <= 0) {
                   $(".resultDataSelect").find(".multiSelectButton").addClass("ng-invalid");
               } else {
                   $(".resultDataSelect").find(".multiSelectButton").removeClass("ng-invalid");
               }
           });
like image 40
DivineOps Avatar answered Dec 28 '22 05:12

DivineOps