checkbox mandatory in widget

pritimayee
Tera Contributor

Can anyone please help me on the below issue.

I have created one widget and in the widget i have created the checkboxes like the below.

find_real_file.png

I have created the variable "this request is" custom with lable type and attached the widget. Need to make mandatory the widget so that if anyone out of the four box is checked the request can be submitted or else the request will not get submit.

7 REPLIES 7

Hello Ankur,

 

I need to give link for different catalog item like below.SO used widget for this.

find_real_file.png

Hi,

then you can use approach mentioned by Markus

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Markus Kraus
Kilo Sage

You need the following artifacts for this (i assumed that the checkbox configuration is stored as JSON):
find_real_file.png

Widget HTML:

<div>
	<input type="checkbox" ng-model="checkboxConfig.checkbox1" ng-change="update()" />
  	<input type="checkbox" ng-model="checkboxConfig.checkbox2" ng-change="update()" />
</div>



Widget Client controller:

api.controller=function($scope) {
	// handle 'Default value' and external sources
	// e.g. a client script has g_form.setValue('our field', true)
	$scope.$watch('page.field.value', function (newValue, oldValue, scope) {
		$scope.checkboxConfig = JSON.parse(newValue);
	});
	
	// mandatoryness does not work OOTB with custom widgets!
	// this is *only* for UI
	$scope.page.field.mandatory = true;
	
	$scope.page.field.mandatory_filled = function () {
		var numChecked = 0;
		for (var checkbox in $scope.checkboxConfig) {
			if ($scope.checkboxConfig[checkbox]) {
				numChecked++;
			}
		}
		
		return numChecked > 0;
	};

	$scope.update = function () {		
		$scope.page.g_form.setValue($scope.page.field.variable_name, JSON.stringify($scope.checkboxConfig));
	};
};


The mandatory handling however is just UI stuff, you still need a onSubmit Catalog Client Script, and i guess there is no way around that (unfortunately):

UI Type: All
Script:

function onSubmit() {
	var checkboxes = JSON.parse(g_form.getValue('checkboxes'));
	for (var checkbox in checkboxes) {
		if (checkboxes[checkbox]) {
			return true;
		}
	}

	g_form.addErrorMessage('At least one checkbox has to be selected!');
	return false;
}