1

I have only one input Field in my Component i.e

lightning:input aura:id="AccId" label="Enter Account Name" name="SearchAccount" 
 value="{!v.searchtext}" placeholder="Search for an Account.../><br/>

lightning:button label="Search"
   variant="brand"
 onclick="{!c.searchAccount}"/>"

My requirement is - When user clicks on Search Button and if the Input field is Empty .. It should throw an error saying that Account Name is required(or any Custom message).

NOTE:- I do not want to go with required attribute as it throws very standard message and that to when user clicks in input text field and comes out of the field ..

1

Yes you can do so by adding attribute messageWhenValueMissing

<lightning:input aura:id="AccId" label="Enter Account Name" name="SearchAccount" 
 value="{!v.searchtext}" placeholder="Search for an Account..." messageWhenValueMissing="Your message" required="true"/>
    <lightning:button label="Search"
   variant="brand"
 onclick="{!c.searchAccount}"/>

Then in js add

searchAccount : function(component, event, helper) {
    //your criteria
    component.find('AccId').showHelpMessageIfInvalid();
}

you can find more attributes at https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/aura_compref_lightning_input.htm

they had given an example and different type of errorMessage we can show

You can override the default messages by providing your own values for these attributes: messageWhenBadInput, messageWhenPatternMismatch, messageWhenTypeMismatch, messageWhenValueMissing, messageWhenRangeOverflow, messageWhenRangeUnderflow, messageWhenStepMismatch, messageWhenTooLong.

| improve this answer | |
  • Thank you Manjot. This solution will give Error if I click on Search button and as well as If I click in input field and come out of that field without Entering any text. I do not want to throw error when user clicks in Input field and comes out of that field without Entering anything there. It should throw an error only when user clicks on Search Button. This is what I mentioned in my Question and Still not able to figure out. – sumit pandey Jan 29 '18 at 9:08
  • how did above worked :p ? – Manjot Singh Jan 29 '18 at 14:58
0

Check out lightning:recordEditForm and lightning:inputField. They handle this for you. Documentation is at https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/aura_compref_lightning_inputField.htm

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.