0

I am trying to set a validation on a dynamic lightning:inputfield bus this is not working neighter do I get an error. I think my binding isn't correct.

Component

<aura:iteration items="{!v.fields}" var="field" >
                <aura:if isTrue="{!v.inputModeBool == true? true: false}">
                    <lightning:inputField fieldName="{!field.fieldPath}"  aura:id="{!field.fieldPath}"/>{!field.required}
                </aura:if>
                <aura:if isTrue="{!v.inputModeBool != true? true: false}">
                    <lightning:layout >
                        <lightning:layoutitem size="10">
                            <lightning:outputField fieldName="{!field.fieldPath}"/>
                        </lightning:layoutitem>
                        <lightning:layoutitem size="2">
                            <lightning:buttonIcon iconName="utility:edit" variant="bare"
                                                  alternativeText="edit"
                                                  onclick="{!c.next}"/>
                        </lightning:layoutitem>
                    </lightning:layout>
                </aura:if>
            </aura:iteration>

Controller

   next: function(component, event, helper) {
         alert('Whoohooo');
       var invalidFields = helper.isFormValid(component);
    if(invalidFields && invalidFields.length > 0){
        helper.reportUserError(
            'Please complete all required fields',
            'Incomplete: '+ invalidFields.join(', ')
        );
        event.preventDefault();
        return;
    }
    },

Helper

    isFormValid: function (component) {
    return (component.find('field.fieldPath') || [])
        .filter(function (i) {
            var value = i.get('v.value');
            return !value || value == '' || value.trim().length === 0;
        })
        .map(function (i) {
            return i.get('v.fieldName');
        });
}
0
  1. lightning:inputField cannot be used without recordEditForm.
  2. aura:id cannot be dynamic. It should be static. That is, you cannot mention it as {!dynamicValue}. You rather give it static value like "myInputField"
  3. You cannot check validity in inputField, you can check custom validity on lightning:input. Documentation says:

Usage Considerations To persist requiredness on a field, select the Required checkbox when defining the custom field in Setup. A required field is displayed with a red asterisk next to the field label. An error message is displayed below a required field if you try to submit the form before entering a value.

By making aura:id as static, you can find it. If there is single element with that aura:id, it will return single element. If there there are multiple elements with same name, (like in iteration) then it will return array of elements. Ref: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_ids.htm

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.