One checkbox should be selected from multiple checkboxs with different ng-model - angularjs

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

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

Related

Pass array just what user selects

I want to pass data what user checked. My problem now.. it pass all..
This is my demo code and stackblitz
HTML
<div class="row" *ngFor="let item of modules; let i = index;">
<div class="col-md-1 align-center">{{i+1}}</div>
<div class="col-md-5">
<input type="text" class="form-control" [(ngModel)]="modules[i].module_name" value="{{modules[i].module_name}}" disabled>
</div>
<div class="col-md-2">
<input type="radio" class="form-control" [checked]="modules[i].action.read" (change)="modules[i].action.read" name="access_{{modules[i].company_id}}" id="package-1">
<label for="package-1">Read</label>
</div>
<div class="col-md-2">
<input type="radio" [checked]="modules[i].action.write" (change)="modules[i].action.write" class="form-control" name="access_{{modules[i].company_id}}" id="package-2">
<label for="package-2">write</label>
</div>
<button (click)="test(item)">test</button>
</div>
Component
test(val){
console.log(val)
}
There are two issues mainly:
You need to create a unique id for both label and id attributes within the for-loop.
Next, create a method that will toggle the read/write property of each individual module. This ensures that if read is set then write will be false and vice versa.
.ts
toggleReadWrite(module: any, isRead: boolean) {
if (isRead) {
module.action.read = !module.action.read;
module.action.write = false;
} else {
module.action.write = !module.action.write;
module.action.read = false;
}
}
.html
<div class="row" *ngFor="let item of modules; let i = index;">
<div class="col-md-1 align-center">{{i+1}}</div>
<div class="col-md-5">
<input type="text" class="form-control" [(ngModel)]="modules[i].module_name"
value="{{modules[i].module_name}}" disabled>
</div>
<div class="col-md-2">
<input type="radio" class="form-control" [checked]="modules[i].action.read"
(change)="toggleReadWrite(modules[i], true)"
name="access_{{modules[i].company_id}}" id="package+1+{{i}}">
<label for="package+1+{{i}}">Read</label>
</div>
<div class="col-md-2">
<input type="radio" [checked]="modules[i].action.write"
(change)="toggleReadWrite(modules[i], false)" class="form-control"
name="access_{{modules[i].company_id}}" id="package+2+{{i}}">
<label for="package+2+{{i}}">write</label>
</div>
<button (click)="test(item)">test</button>
</div>
First of all, you should make the id of radio buttons unique as you are looping through it.
id="package-1{{i}}"
<input type="radio" class="form-control" [checked]="modules[i].action.read" (change)="modules[i].action.read" name="access_{{modules[i].company_id}}"
id="package-1{{i}}">
<label for="package-1{{i}}">Read</label>
Also, I am not sure what is your desired output, please explain more.

Vue.js multiple check box

I have 3 check boxes . Want to get result like {ceheckboxName1:value,ceheckboxName2:value,ceheckboxName3:value}.You can see my code for better understand.
<li>
<input name="" type="checkbox" value="100" v-model="checkedNames" id="incall">
<label for="incall">Incall</label>
<div class="pull-right price-now-right"><p>100</p></div>
</li>
<li>
<input name="" type="checkbox" value="200" v-model="checkedNames" id="">
<label for="lbl">view</label>
<div class="pull-right price-now-right"><p>100</p></div>
</li>
<li>
<input name="" type="checkbox" value="On Request" v-model="checkedNames" id="overnight">
<label for="ovr">lmm</label>
<div class="pull-right price-now-right"><span>On request</span></div>
</li>
Give each checkbox a :value="true" (yes, the : is important) and v-model="checkboxName1" (2, 3, etc.). That will automatically set this.checkboxName1 to true if checked.
Then, in your data function:
data() {
return {
checkboxName1: false,
checkboxName2: false,
checkboxName3: false,
};
}
this.$data will contain your desired format.

Angular hide/show based on radio button

Plunker
How can i make yes radio button to be checked on page load and show/hide the field-set accordingly. Currently on load radio button is checked but its not showing the fieldset.
Html
<form name="myform" class="form " novalidate >
<fieldset>
<span>Would you like a receipt sent to you?</span>
<ul class="vList">
<li>
<input type="radio" name="receiptConfirmation" id="receiptAffirm" ng-model="reciept" value="yes" ng-checked="true">
<label class="form__radioLabel" for="receiptAffirm:" >Yes</label>
</li>
<li>
<input type="radio" name="receiptConfirmation" id="receiptDeny" ng-model="reciept" value="no">
<label class="form__radioLabel" for="receiptDeny">No </label>
</li>
</ul>
</fieldset>
<fieldset class="fieldset" ng-show="reciept=='yes'">
<legend class="isVisuallyHidden">Email Receipt</legend>
<ul class="vList">
<li>
<label for="firstName" class="form__label">First Name</label>
<input id="Text4" class="form__input form__input--textfield" type="text" >
</li>
<li>
<label for="emailAddress" class="form__label">Email Address</label>
<input id="email1" class="form__input form__input--textfield" type="email">
</li>
</ul>
</fieldset>
</form>
Do not mix ng-checked with ng-model. Setting ng-checked will not update the model, it will just update the element's checked property. Instead set the ng-model reciept to the desired value.
Remove ng-checked from input:-
<input type="radio" name="receiptConfirmation" id="receiptAffirm"
ng-model="reciept" value="yes">
In your controller set the model:-
app.controller('MainCtrl', function($scope) {
$scope.reciept = "yes";
});
Demo
Just set $scope.reciept = 'yes'; in your controller?
You have spelt receipt wrong also.

Getting Forms to work correctly in Angular

I can never seem to get Forms to work properly in Angular. I always add a <form name="myform"> and then try to access it either in the template or in the controller using $scope.myform but it is always undefined! I can never seem to figure out exactly how these work.
Here is my code:
Template
<form name="o365form" novalidate>
<ul class="formList">
<li>
<label>Office 365 Admin Username</label>
<input type="text" ng-model="Master.Start.skAccount.username" required/>
</li>
<li>
<label>Office 365 Admin Password</label>
<input type="text" ng-model="Master.Start.skAccount.password" required/>
</li>
</ul>
</form>
Then I have this code in a function in my controller, but o365form is undefined...
Controller
if ($scope.o365form.$invalid) {
return false;
} else {
return true;
}
You need to use to ng-form directive with this:
This is as simple as
<ng-form name="o365form">
<ul class="formList">
<li>
<label>Office 365 Admin Username</label>
<input type="text" ng-model="Master.Start.skAccount.username" required/>
</li>
<li>
<label>Office 365 Admin Password</label>
<input type="text" ng-model="Master.Start.skAccount.password" required/>
</li>
</ul>
</ng-form>
Add ng-submit directive in form
<form name="o365form" ng-submit="submit(o365form)" novalidate>
<ul class="formList">
<li>
<label>Office 365 Admin Username</label>
<input type="text" ng-model="Master.Start.skAccount.username" required/>
</li>
<li>
<label>Office 365 Admin Password</label>
<input type="text" ng-model="Master.Start.skAccount.password" required/>
</li>
</ul>
</form>
in controller
$scope.submit = function(o365form){
if (o365form.$valid) {
return false;
} else {
return true;
}
}
Example with $watch
<form name="o365form" form-valid novalidate>
<ul class="formList">
<li>
<label>Office 365 Admin Username</label>
<input type="text" ng-model="o365form.username" required/>
</li>
<li>
<label>Office 365 Admin Password</label>
<input type="text" ng-model="o365form.password" required/>
</li>
</ul>
<button type="submit">Submit</button>
</form>
Directive
app.directive("formValid", function () {
return {
link: function ($scope, element, attrs){
$scope.$watch(attrs.name, function (isValid){
if (isValid.$valid) alert("valid");
else alert("not valid");
}, true);
}
}
});
DEMO

Can ng-disabled disable group of child elements?

I am trying to disable a group of element using ng-disabled in its parent element. Doesn't seem to work.
Does it mean I have to using same condition in all child element to disable ?
I am not able to disable all elements by putting ng-disabled="true" with ul or li element. It works only when I put at input element. Is this how it works ? Also ng-disabled="true" doesn't work with lable or span !
<ul style="list-style:none">
<li>
<input type="radio" id="radio1" ng-model="color" value="red">
<label for="radio1" class=""><span>Red</span></label>
</li>
<li>
<input type="radio" id="radio2" ng-model="color" value="green">
<label for="radio2" class=""><span>Green</span></label>
</li>
<li>
<input type="radio" id="radio3" ng-model="color" value="blue">
<label for="radio3" class=""><span>Blue</span></label>
</li>
fiddle : http://jsfiddle.net/chiranjib_halder1/pqvz9veu/3/
You can use this directive:
https://github.com/pleerock/angular-disable-all.git
that basicaly picks the elements inside the directive and disable them.
here is your fiddle code using this directive:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="https://cdn.rawgit.com/pleerock/angular-disable-all/master/dist/angular-disable-all.min.js"></script>
<body ng-app="radioExample">
<script>
angular.module('radioExample', ['disableAll'])
.controller('ExampleController', ['$scope', function($scope) {
$scope.color = 'blue';
$scope.specialValue = {
"id": "12345",
"value": "green"
};
}]);
</script>
<div ng-controller="ExampleController">
<input type="checkbox" ng-model="isDisabled">Disable all<br>
<ul style="list-style:none" disable-all="isDisabled">
<li>
<input type="radio" id="radio1" ng-model="color" value="red">
<label for="radio1" class=""><span>Red</span></label>
</li>
<li>
<input type="radio" id="radio2" ng-model="color" value="green">
<label for="radio2" class=""><span>Green</span></label>
</li>
<li>
<input type="radio" id="radio3" ng-model="color" value="blue">
<label for="radio3" class=""><span>Blue</span></label>
</li>
<tt>color = {{color | json}}</tt><br/>
<button type="button">Click Me!</button>
</ul>
</div>
</body>
ng-disabled is used on input elements only. here, "checked" is used to enable the button after the checkbox has been checked. here is a hypothetic example:
Click me to toggle: <input type="checkbox" ng-model="checked"><br/>
<button ng-model="button" ng-disabled="checked">Button</button>
https://docs.angularjs.org/api/ng/directive/ngDisabled

Resources