How to make a required `lightning-input-field` field not required?

Is it possible to make a <lightning-input-field> where field-name is required (such as Contact.LastName) not required on the UI?

I would like the field to not kick in validation rules (client ones) and not to have the required styling once I submit, but it still happens when setting required to false.

Sample code:

    <lightning-record-edit-form
      object-api-name="Contact"
      form-class="slds-grid slds-wrap slds-m-left--xx-large slds-m-bottom--medium"
      onsubmit={handleSearch}
    >
      <template for:each={searchFields} for:item="field">
        <div key={field.name} class="slds-col slds-size_1-of-4">
          <lightning-input-field field-name={field.name} value={field.value} required={isFalse}></lightning-input-field>
        </div>
      </template>
      <div class="slds-col_bump-left slds-p-top--large">
        <lightning-button class="slds-p-right--large" label="Search" type="submit"></lightning-button>
      </div>
    </lightning-record-edit-form>

In the JavaScript file:

  get isFalse() {
    return false;
  }

Answer

Since you are using a lightning-record-edit-form, it automatically enforces any validations that salesforce has, in this case the contact.LastName being required.

If you want to make the field ‘not required’ from the UI, you should not be using the edit-form, hence even the lightning-input-field, the alternative to do this would be, to use the ‘lightning-input’ element and mark the required=false, but we need to keep in mind that, if it is required from salesforce, it would throw an error. Since you cannot override the salesforce validations from UI. Hope it helps.

Attribution
Source : Link , Question Author : jpmonette , Answer Author : Awais Ahamed

Leave a Comment