0

I need some help with JavaScript code, please. In Salesforce Communities, I have a form that asks a question, if you answer yes, then more fields appear. I want to set it to if yes, those fields become mandatory.

This part works fine:

  <label for="export">Do you export?</label>
  <ui:inputSelect value="{!v.account.Export__c }" aura:id="export">
    <ui:inputSelectOption text="No" />
    <ui:inputSelectOption text="Yes" />
  </ui:inputSelect>

  <aura:if isTrue="{!v.account.Export__c == 'Yes'}">

I would like to make the below mandatory only if yes is selected:

    <label for="export-where">Where do you export?</label>
      <ui:inputSelect multiple="true" aura:id="export-where" value=" !v.account.Export_to_where__c}" change="{!c.handleExports}">
        <ui:inputSelectOption text="Option 1" />
        <ui:inputSelectOption text="Option 2" />
        <ui:inputSelectOption text="Option 3" />
       </ui:inputSelect>

      <label for="export-what">What do you export?</label>
        <ui:inputTextArea aura:id="export-what" value="{!v.account.Export_what__c}" maxlength="32768" />                          
    </aura:if>

The below JavaScript works as all being mandatory. How do I change it so that when the Export_to_where__c question is filled it validates?

else if(getCurrentStep == 3){
    this.removeBorder(component, 'export');
    if((account.Export__c == 'No')) {
        return true;
    }
else{
    component.set("v.errorMessage","* Please fill all the required details");
   if(account.Export__c =='Yes' || account.Export_to_where__c == 'undefined' || account.Export_to_where__c == null || account.Export_to_where__c.trim() == '') {    
        this.addBorder(component, 'export-where');
    }
}

Any help would be appreciated.

0

I would like to suggest a simpler way below. Example:

    <aura:application extends="force:slds">

        <aura:attribute name="val" type="String" default=""/>
        <aura:attribute name="textval" type="String" default=""/>

        <ui:inputSelect value="{!v.val }" aura:id="export">
            <ui:inputSelectOption text="No" />
            <ui:inputSelectOption text="Yes" />
        </ui:inputSelect>
        <br/>

<ui:inputSelect multiple="true" aura:id="export-where" value=" !v.account.Export_to_where__c}" required="{! v.val=='Yes'}">
        <ui:inputSelectOption text="Option 1" />
        <ui:inputSelectOption text="Option 2" />
        <ui:inputSelectOption text="Option 3" />
       </ui:inputSelect>

        <ui:inputTextArea value="{!v.textval}" required="{! v.val=='Yes'}" label="text"/> 

    </aura:application>

Hope it helps.

  • Thanks, but this looks like tit would only validate the yes or no, which isn't what I need. – jesstp Aug 28 '19 at 7:00
  • Sorry, wasn't clear to me much, are you trying to make option group upon Yes/No question and if they click on option group. – Pasan Eeriyagama Aug 28 '19 at 23:14
  • If yes is selected, then 2 more fields appear. Those fields need to be mandatory ONLY if yes is chosen. If No is selected then the user continues on there journey. – jesstp Aug 28 '19 at 23:27
  • I think my example answer above would help you to change the markup, and use less coding to achieve that. Not sure if you can use required="{!condition}" attribute to achieve it.Updated the example little bit more. Hope it helps. Cheers! – Pasan Eeriyagama Aug 29 '19 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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