3

How to validate checkbox with different ng-model??

For e.g:

My HTML looks like:

<form name="myFrm">
  <ul>
      <li><input type="checkbox" ng-model="sunday" />Sunday</li>
      <li><input type="checkbox" ng-model="monday" />Monday</li>
      <li><input type="checkbox" ng-model="tuesday" />Tuesday</li>
      <li><input type="checkbox" ng-model="wednesday" />Wednesday</li>
      <li><input type="checkbox" ng-model="thursday" />Thursday</li>
      <li><input type="checkbox" ng-model="friday" />Friday</li>
      <li><input type="checkbox" ng-model="saturday" />Saturday</li>
  </ul>

  <p ng-if="myFrm.$invalid">Atleast one day should be selected</p>
</form>

from the above code, i want to check atleast 1 checkbox should be selected OR give error message.

I've tried searching, but i found the solution in which has a same ng-model, but i have different ng-model for each checkbox.

How to do that??

DEMO JS FIDDLE

Jay
  • 3,353
  • 5
  • 25
  • 34
  • You want a custom validation, which is discussed in detail [here](http://stackoverflow.com/questions/12581439/how-to-add-custom-validation-to-an-angular-js-form). – dave walker Jun 20 '14 at 11:55

1 Answers1

5

Try creating custom directives to do custom validation:

//This directive is to update Form validity when any of the elements decorated
// with customRequired is not empty.
app.directive("customRequiredContainer",function(){
  return {
    restrict:"A",
    require:"form",
    controller:function($element,$scope){
      var properties = []; //store the list of properties to check. 

      //customRequired will register the property to be checked.
      this.registerProperty = function(property){
       if (properties.indexOf(property) === -1){
           properties.push(property);

          $scope.$watch(property,function(value){
            if ($element.form){

              //If any of the elements is checked, Form is valid otherwise not valid.
              for (var i=0;i<properties.length;i++){
                if ($scope[properties[i]]){
                  //we should use $setValidity(), 
                  //I don't know why it does not work, check that later.
                  $element.form.$invalid = false;
                  $element.form.$valid = true;
                  return;
                }
              }

              $element.form.$invalid = true;
              $element.form.$valid = false;
            }
          });
        }
      };
    },
    link:function(scope,element,attrs,formController){
      element.form = formController;
    }
  }
});

//This directive is to decorate which element should be checked for validity
app.directive("customRequired",function(){
  return {
    restrict:"A",
    require:"^customRequiredContainer",
    link:function(scope,element,attrs,containerController){
      containerController.registerProperty(attrs.ngModel);
    }
  }
});

HTML:

<form name="myFrm" custom-required-container>
    <ul>
      <li>
        <input type="checkbox" ng-model="sunday" custom-required/>Sunday</li>
      <li>
        <input type="checkbox" ng-model="monday" custom-required/>Monday</li>
      <li>
        <input type="checkbox" ng-model="tuesday" custom-required/>Tuesday</li>
      <li>
        <input type="checkbox" ng-model="wednesday" custom-required/>Wednesday</li>
      <li>
        <input type="checkbox"  ng-model="thursday"  custom-required/>Thursday</li>
      <li>
        <input type="checkbox"  ng-model="friday" custom-required />Friday</li>
      <li>
        <input type="checkbox"  ng-model="saturday" custom-required />Saturday</li>
    </ul>

    <p ng-if="myFrm.$invalid">Atleast one day should be selected</p>
  </form>

DEMO

Or this:

<form name="myFrm">
    <ul>
      <li>
        <input type="checkbox" name="sunday" ng-model="sunday" required/>Sunday</li>
      <li>
        <input type="checkbox" name="monday" ng-model="monday" required />Monday</li>
      <li>
        <input type="checkbox" name="tuesday" ng-model="tuesday" required />Tuesday</li>
      <li>
        <input type="checkbox" name="wednesday" ng-model="wednesday" required />Wednesday</li>
      <li>
        <input type="checkbox" name="thursday" ng-model="thursday" required />Thursday</li>
      <li>
        <input type="checkbox" name="friday" ng-model="friday" required />Friday</li>
      <li>
        <input type="checkbox" name="saturday" ng-model="saturday" required />Saturday</li>
    </ul>

    <p ng-if="myFrm.sunday.$error.required&&myFrm.monday.$error.required
    &&myFrm.tuesday.$error.required&&myFrm.wednesday.$error.required
    &&myFrm.thursday.$error.required&&myFrm.friday.$error.required
    &&myFrm.saturday.$error.required">Atleast one day should be selected</p>
 </form>

DEMO

Explanation:

By giving the inputs names: name="sunday", angular will add the input name as a property of the form: myFrm.sunday. From then, we can check whether the input is selected using its $error.required property.

Or this:

<form name="myFrm">
    <ul>
      <li>
        <input type="checkbox" ng-model="sunday" />Sunday</li>
      <li>
        <input type="checkbox" ng-model="monday" />Monday</li>
      <li>
        <input type="checkbox" ng-model="tuesday" />Tuesday</li>
      <li>
        <input type="checkbox" ng-model="wednesday" />Wednesday</li>
      <li>
        <input type="checkbox"  ng-model="thursday"  />Thursday</li>
      <li>
        <input type="checkbox"  ng-model="friday"  />Friday</li>
      <li>
        <input type="checkbox"  ng-model="saturday"  />Saturday</li>
    </ul>

    <p ng-if="!sunday&&!monday&&!tuesday&&!wednesday&&
    !thursday&&!friday&&!saturday">Atleast one day should be selected</p>
  </form>

DEMO

Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • @Khanh TO thanx for the solution, it worked but there is only 1 problem, when 1 checkbox is selected error message goes but other checkbox still has the RED border around it, how to remove that RED border? – Jay Jun 20 '14 at 11:38
  • @Jay: how do you style your css? – Khanh TO Jun 20 '14 at 11:42
  • @KhanhTO there is no CSS for that... and also you have kept each checkbox as required, because of that my submit button gets disabled because i am checking on submit button like 'save' – Jay Jun 20 '14 at 11:44
  • @Jay: See my update for a quick solution if you only need to show/hide error message. – Khanh TO Jun 20 '14 at 11:50
  • @KhanhTO i want to show/hide the error message as well as keep the save button disable if none of checkbox is selected. – Jay Jun 20 '14 at 11:52
  • @Jay: just use `save` or write a function `isValid()` to use in both places. – Khanh TO Jun 20 '14 at 11:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55981/discussion-between-jay-and-khanh-to). – Jay Jun 20 '14 at 11:57