I am trying to place a custom validation if atleast one of my checkboxes is selected or not. and show the relevant error message.
I am able to achieve this when there is only one checkbox , HOWEVER when there are TWO or more checkboxes coming , it does not work. It simply says inputField.set is not a function or inputField.setCustomValidity is not a function when debug goes there.
My research (apart from debugging) is as follows:- Getting a Proxy object instead of an actual component attribute
lightning checkbox is not working
My component(view code) code is as follows:-
<aura:attribute name="INPUTTypeList" type="List" default="[]"/>
<div class="slds-col--padded-medium ">
<span class="slds-m-bottom--xx-small">My INPUT Programs</span>
<div style="display: -webkit-inline-box;">
<aura:iteration items="{!v.Common.INPUTType}"
var="item">
<!-- not mentioning iteration logic as value is set from other component
and code here would become verbose, just for understandinding it is a list
coming from other component -->
<lightning:input type="checkbox" label = "{!item}"
aura:id="INPUTtype" value="{!item}" onchange="
{!c.handleChangeINPUTType}"
/>
</aura:iteration>
</div>
</div>
Controller Code is as follows
handleChangeINPUTType: function (component, event) {
var capturedCheckboxName = event.getSource().get("v.value");
var selectedCheckBoxes = component.get("v.INPUTTypeList");
if(selectedCheckBoxes.indexOf(capturedCheckboxName) > -1){
selectedCheckBoxes.splice(selectedCheckBoxes.indexOf( capturedCheckboxName
), 1);
}
else{
selectedCheckBoxes.push(capturedCheckboxName);
}
component.set("v.INPUTTypeList", selectedCheckBoxes);
//doubt STARTS HHERE
if(selectedCheckBoxes.length <1)
{
// Debug comes here and works FINE only for one checkbox
// debug comes here and THROWS EXCEPTION in case of 2 checkboxes
// IT THROWS AN EXCEPTION AS SOON AS it encounters 'inputField'
var inputField = component.find('INPUTtype');
inputField.set('v.validity','false');
inputField.setCustomValidity("Select atleast one INPUT type");
inputField.reportValidity();
}
else {
inputField.set('v.validity','true');
inputField.setCustomValidity('');
inputField.reportValidity();
}
//doubt ends HHERE
},
inputField
in your JS Controller, where is it defined?