function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vikas rathi91vikas rathi91 

validate child component from parent componet

We have created a lightning component. Inisde the component we are refering another component. we would like to validate the data inside child component on a button click from parent component.

Can anyone give some sample code or suggestion?

Thanks and RegardsUser-added image
Here we call child component

<div class="slds-modal__content slds-p-around--medium wide400">
                <!--Transaction Editor Component Here -->
                <!--<c:TransactionViewer thistrans="{!v.thistrans}" currencies="{!v.currencies}" 
                           tdetails="{!v.thisdetails}" /> -->
             {!v.body}
</div>


below is component  code is long so I paset only 2 field code


<div class="slds-form-element slds-size--1-of-3">
                            <!--<label class="slds-form-element__label" for="descrField">Description</label>-->
                            <!--<force:inputField aura:id="descrField" class="slds-textarea" value="{!v.thistrans.Description__c}"/> -->
                            <ui:inputText aura:id="descrField" class="slds-input" required="true" label="Description" value="{!v.thistrans.Description__c}"/>
                        </div>
                        <div class="slds-form-element slds-size--1-of-3">                            
                            <c:LookupSObject label="Deal/Tranche" pluralLabel="Deal/Tranches" sObjectAPIName="Tranche__c" instanceId="tnxTr"                          
                                             
                                             iconName="standard:process"
                                             listIconClass="slds-icon-standard-account"
                                             selectedId="{!v.thistrans.Tranche__c}"
                                             selectedName="{!v.thistrans.TName}"
                                             />
                        </div>

And here is controller code
 
okTranerror: function(component){
        var tratitle = component.find("trannameField").get("v.value");
        var haserrors = false;
        
        if(!tratitle){            
            effnamefld.set("v.errors", [{message:"Provide Name First!"}]);
            haserrors = true;
        }
please help i m try a lot but not success

thanks in advance 
Ganesh Babu 8Ganesh Babu 8
Hello Accky,

You can use events to alert the parent component on validation. You can use events for all actions in child component.

eg: onclick, onhover.. etc.

Thanks and Regards,
Ganesh Babu.
vikas rathi91vikas rathi91
Hello Ganesh,

Thanks for reply. I am done the validation without the event actions. now I am not able to show error on the field that will be indicate that the value is null. I am using events on that to show the error but not success. 
pls help

below is code for component, client side controller, and helper to show the error where is problem i m not find 
<ui:inputText aura:id="trannameField" class="slds-input" required="true" label="Transaction Title" value="{!v.thistrans.Name}" mouseout="{!c.handleClick}" />





 handleClick : function(component, event, helper) {   
        debugger;
        helper.handleClick(component);





({
	handleClick : function(component) {
        var traname = component.find("trannameField").get("v.value");
		
        var haserrors = false;
        
        if(!traname){            
            traname.set("v.errors", [{message:"Provide Name First!"}]);
            haserrors = true;
        } 
        if(haserrors) return;
	}
})

thanks
Fred13Fred13
Ganesh, did you ever find a solution?  If so, could you please share?   Thanks!!