I have a series of rows with one checkbox per. With each row are several inputs. I already have validation that requires at least one checkbox in all the rows be checked. However, I am thus far unable to require the inputs have a value ONLY for the checkbox(es) checked.
The HTML:
<div style="width: 100%;">
<checkboxgroup-Wheelbase min-required="1">
<table style="width: 100%">
<tr>
<td>
<table style="width: 97%;">
<tr>
<th style="width: 220px;" class="wheelbaseHeaderCell">Wheelbase<br /><span style="font-weight: 400;">(choose min of 1)</span></th>
<th class="wheelbaseHeaderCell">Payload<br />[ pounds ]</th>
<th class="wheelbaseHeaderCell">Length<br />[ inches ]</th>
<th class="wheelbaseHeaderCell">Height<br />[ inches ]</th>
<th class="wheelbaseHeaderCell">Weight<br />[ pounds ]</th>
<th class="wheelbaseHeaderCell">Turn Radius<br />[ feet ]</th>
</tr>
</table>
</td>
</tr>
<tr>
<td style="height: 5px;"></td>
</tr>
<tr>
<td>
<div style="height: 170px; overflow: auto;">
<table style="width: 100%;">
<tr class="rowGroup" ng-repeat="wheelbase in wheelbases" data-rowParent="wheelbase[wheelbase.Id]">
<td class="wheelbaseCell" style="padding-left: 10px;" id="{{wheelbase.Id}}">
<!--Wheelbase-->
<label class="checkbox" for="{{wheelbase.Id}}" style="text-align: left; width: 200px;">
{{wheelbase.WheelbaseGrade}} - {{wheelbase.Inches}} inches
<input ng-model="wheelbase.checked" id="wheelbase{{wheelbase.Id}}" type="checkbox" /></label>
</td>
<td >
<!--Payload Capacity-->
<span style="display: inline-block;" ng-controller="PayloadCapacitiesCtrl">
<input ng-model="payloadCapacity.Pounds" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked" />
</span>
</td>
<td >
<!--Length-->
<span style="display: inline-block;" ng-controller="VehicleLengthCtrl">
<input data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked"/>
</span>
</td>
<td >
<!--Height-->
<span style="display: inline-block;" ng-controller="VehicleHeightCtrl">
<input data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked"/>
</span>
</td>
<td >
<!--Weight-->
<span style="display: inline-block;" ng-controller="VehicleWeightCtrl">
<input data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked"/>
</span>
</td>
<td >
<!--Turning Radii -->
<span style="display: inline-block;" ng-controller="TurningRadiiCtrl">
<input data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked"/>
</span>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</checkboxgroup-Wheelbase>
<span class="hint" style="margin: 0 0 0 -35px; text-align: center; width: 100%;">
<button style="padding-top: 5px; ;" class="addNew" ng-click="openDialog()"><i class="icon-plus"></i> [ add new wheelbase ]</button>
</span>
The directive requiring at least one checkbox be checked:
.directive('checkboxgroupWheelbase', function() {
return {
restrict: 'E',
controller: function($scope, $attrs) {
var self = this;
var ngModels = [];
var minRequired;
self.validate = function() {
var checkedCount = 0;
angular.forEach(ngModels, function(ngModel) {
if (ngModel.$modelValue) {
checkedCount++;
}
});
console.log('minRequired', minRequired);
console.log('checkedCount', checkedCount);
var minRequiredValidity = checkedCount >= minRequired;
angular.forEach(ngModels, function(ngModel) {
ngModel.$setValidity('checkboxgroupWheelbase-minRequired', minRequiredValidity, self);
});
};
self.register = function(ngModel) {
ngModels.push(ngModel);
};
self.deregister = function(ngModel) {
var index = this.ngModels.indexOf(ngModel);
if (index != -1) {
this.ngModels.splice(index, 1);
}
};
$scope.$watch($attrs.minRequired, function(value) {
minRequired = parseInt(value, 10);
self.validate();
});
}
};
})
Is there a simple, elegant way to require the input fields of those checkboxes checked be required? It appears straight forward in JQuery via "rules" but I have not found a way to do this thru AngularJs. Someone suggested to me to use sub-forms but I cannot envision its implimentation.
In Case I was not Clear Enough:
For any row to which the respective checkbox is checked, all the inputs of that row are required to have a value. So, If the checkboxes of say rows 1,3 and 5 are checked, then the inputs of rows 1,3 and 5 require value. If a row's checkbox is checked, that same row's input require a value. If that row's checkbox is not checked, those inputs are not required. And in clearing that up, it made me think a wise thing would be for all inputs to be disabled till their respective wheelbase checkbox is checked.
Update:
I would like to thank user2104976 for making me think about a better user experience insofar as making sure input values are not added when the respective wheelbase checkbox is not checked, so I implemented this
**"ng-disabled="!wheelbase.checked"**
in each of the respective inputs. Thanks dude!! Now how about solving my original problem, LOL!!
You can use ng-required to accomplish this:
ng-required="wheelbase.checked"
Here's the updated HTML's pertinent parts:
<table style="width: 100%;">
<tr class="rowGroup" ng-repeat="wheelbase in wheelbases" data-rowParent="wheelbase[wheelbase.Id]" style="padding-top: 10px;">
<td class="wheelbaseCell" style="padding-left: 10px;" id="{{wheelbase.Id}}">
<!--Wheelbase-->
<label class="checkbox" for="{{wheelbase.Id}}" style="text-align: left; width: 200px;">
{{wheelbase.WheelbaseGrade}} - {{wheelbase.Inches}} inches
<input ng-model="wheelbase.checked" id="wheelbase{{wheelbase.Id}}" type="checkbox" /></label>
</td>
<td >
<!--Payload Capacity-->
<span style="display: inline-block;" ng-controller="PayloadCapacitiesCtrl">
<input ng-model="payloadCapacity.Pounds" data-rowChild="{{wheelbase[wheelbase.Id]}}" id="payload{{wheelbase.Id}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
<td >
<!--Length-->
<span style="display: inline-block;" ng-controller="VehicleLengthCtrl">
<input ng-model="vehicleLength.Inches" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
<td >
<!--Height-->
<span style="display: inline-block;" ng-controller="VehicleHeightCtrl">
<input ng-model="vehicleHeight.Inches" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
<td >
<!--Weight-->
<span style="display: inline-block;" ng-controller="VehicleWeightCtrl">
<input ng-model="vehicleWeight.Pounds" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
<td >
<!--Turning Radii -->
<span style="display: inline-block;" ng-controller="TurningRadiiCtrl">
<input ng-model="turningRadius.Feet" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
</tr>
</table>
With ng-disabled="!wheelbase.checked"
With ng-required="wheelbase.checked" & ng-class="{'formRequire' : wheelbase.checked }"
Here is the conditional class that kicks in to the inputs if the respective wheelbase checkbox is checked:
.ng-invalid .formRequire { outline: red solid 3px; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With