0

I have an aura component bundle (let's say B) with a simple form. On submission of the form, redirects to the target URL mentioned in the action attribute.

My question is, When I invoke B from the component A, I will set the component and/ form attributes and I do not want to display component B to the user. Instead, I want form to be submitted automatically on initialization of component B.

Here is the component B:


<aura:component >
    <aura:attribute access="PRIVATE" name="attribute1" type="String" />
    <aura:attribute access="PRIVATE" name="attribute2" type="String" />
    <aura:attribute access="PRIVATE" name="attribute3" type="String" />
    <aura:attribute access="PRIVATE" name="attribute4" type="String" default="1" />
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>

        <div class="slds-m-around--medium slds-hidden">
        <div class="slds-page-header" role="banner">
            <div class="slds-media__body">
                <form  name="testForm" aura:id="viewForm" action="https://test.test123.com" method="post" enctype="application/x-www-form-urlencoded" target="_top" >    
                 <b> Attribute 1: </b><input type="text" name="inputfield1" value="{!v.attribute1}"/><br/><br/>
                 <b> Attribute 2: </b><input type="text" name="inputfield2" value="{!v.attribute2}"/><br/><br/>
                 <b> Attribute 3: </b><input type="text" name="inputfield3" value="{!v.attribute3}"/><br/><br/>
                 <b> Attribute 4: </b><input type="text" name="inputfield1" value="{!v.attribute4}"/><br/><br/>
                    <input type="submit" value="Submit" name="testSubmit"/>
                </form>
            </div>
        </div>
    </div>
</aura:component>

I am looking for some suggestions to auto submit form in lightning component. Any help is greatly appreciated.

  • from the component A, I will set the component and/ form attributes -- If you can do so from Component A, why will you even need to go to Component B then? Can you clarify exactly what you are trying to achieve? – Jayant Das May 28 '19 at 21:10
1

You can't do so directly without cooperation from "B". It is not possible to access a child component's DOM. You would instead need to modify component "B" to have an aura:method, attribute, etc to allow outside access to submit the form. For example:

<aura:attribute name="autoSubmit" type="Boolean" default="false" access="public" />

...

doInit: function(component, event, helper) {
  if(component.get("v.autoSubmit")) {
    component.find("viewForm").submit();
  }
}

Short of having a method designed to do this from within "B", "A" won't be able to automatically submit the form.

| improve this answer | |

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.