Unable to set required attribute for lightning input

auralightninginputrequiredrequired-field

I'm currently looking to change the required attribute based on multiple scenarios.
So, I have a created input field with the required attribute as newAccRequire but by default, it's getting the true value.

But if I hardcoded directly required="false" it's working as expected.

Component

<aura:attribute name="newAccRequire" type="String" default="false"/>
<lightning:input aura:id="createNewAcc" type="String" name="inputAccName" 
                                 label="" value="{!v.leadRecord.Company}" 
                                 required="{!v.newAccRequire}"
                                 />

I have checked any references for this field on controller or helper, but I'm not using this field. Any idea why the field became mandatory automatically.

I have tried the solution but it's breaking in some situations. still, it's setting as true in some situations. any idea?

Screenshot:
enter image description here

My JS controller

console.log('@@@@Befor_exsiting_Search@@@@'+component.get('v.newAccRequire'));
    component.set("v.newAccRequire","false");
console.log('@@@@exsiting_Search@@@@'+component.get('v.newAccRequire'));

Best Answer

The attribute you are referencing should be of type "Boolean" for representing true/false values. In JavaScript, all non-empty strings are truthy, so if you use the String attribute type, it will be behave differently than if you declare the attribute as a Boolean type.