3

I'm building out a question answer component where certain answers will have specific input types and it would be nice to set some key parameters of the input field yet still use the lightning:recordEditForm component. In particular, I am branching off between a text input or a numeric input where the numeric field has min/max values. I can add those parameters to an lightning:inputField but they dont do anything. I can use a lightning:input an the min max works, but it doesnt save. Any idea on how to mix the two?

<lightning:recordEditForm aura:id="caseQuestionAnswer"
                          objectApiName="Case_Question_Answer__c"
                          onsuccess="{!c.handleSuccess}"
                          recordId="{!v.question.Id}">


    <aura:if isTrue="{!empty(v.question.Application_Question__r.Min__c)}">
        <lightning:inputField fieldName="Answer__c" aura:id="caseQuestionAnswer"/>
        <aura:set attribute="else">
            <lightning:input type="number" fieldName="Value__c" aura:id="caseQuestionValue" min="{!v.question.Application_Question__r.Min__c}" max="{!v.question.Application_Question__r.Max__c}"/>
        </aura:set>
    </aura:if>
    <lightning:button aura:id="submit" type="submit" label="Save Answer" class="slds-m-top_medium"/>
</lightning:recordEditForm>
3

The best thing to do for your specific scenario is to 'hide' the lightning:inputField and add a lightning:input, similar to what Uwe Mentioned, however, you dont really need to do anything with the lightning:inputField, you will simply need it to load the data associated to it and to be able to Save the field value on form submission. For Example:

<lightning:recordEditForm aura:id="recordEditForm"
                            onload="{!c.spinnerUi}"
                            onsuccess="{!c.handleRecordSaved}"
                            onsubmit="{!c.handleRecordSubmit}"
                            objectApiName="Case">

    <lightning:messages />


     <lightning:inputField aura:id="subject" fieldName="Subject" style="display:none;"/>
     <lightning:input label="Subject" name="Case Subject" onblur="{!c.itemsChange}"/>

     <lightning:button aura:id="submit" type="submit" label="Create Case" class="slds-m-top_medium"/>

</lightning:recordEditForm> 

When you submit your form, use the onsubmit event to fetch the field value form the input field (not the lighntning:inputField) and assign it to the fields object:

event.getParams().fields.FieldName= //Get Value Here//;
  • I have not been able to successfully capture the submit event and add values to fields. There was another question on here about this technique, which I can't find right now, using event params and object info to set the lightning data service field value but I wasnt able to get it working. Do you have a link to something that does work? event.preventDefault(); var fields = event.getParam("fields"); fields["Applicant__c"] = applicant.userId; // contact id component.find("newForm").submit(fields); // Submit form – textual Aug 31 '18 at 16:29
  • I think you should better open a new post, alongside a minimal code samples of how you are attempting to do so, alongisde the current and expected behavior. – glls Aug 31 '18 at 16:35
  • heres the solution for saving on submit. that, coupled with your field swap worked like a charm. now I just need to populate the field on load as well. thanks for your help! – textual Aug 31 '18 at 17:00
2

The lightning:recordEditForm is usind Data Service to load and save to the database. This can only work, if you use the lighting:inputField sub components.

So to go on, you could save just the Value__c field of the record in your own APEX code. But you need to be careful: the record will be saved twice: once by Data Service and once by your custom APEX. Due to both operating asynchroniosly I would expect issues.

Alternatively you could try to fetch all the values from the lighning:recordEditForm and all it's inputFields and do all the save operation in your custom APEX.

Or you could try to use JS to update an invisible lightning:inputField with the value of you lightning:input but I think there are curtently issues/bugs with the lightning:inputField giving you a hard time to do so.

Bottomline

Unfortunately the current implementation of lightning:inputField isn't as flexible as I would like it to be. Lot's of the constraints are possibly existing because of it's design to use Data Service. It makes easy things very easy and you don't need any APEX to write to the database. As downside the mechanism is limited and not very flexible.

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.