Retain input values while validating custom Flow Screen Lightning components – Part 2

Hello and welcome back! 😀

In our previous post, we discussed how we can validate custom flow screen components, and towards the end of it we also touched upon the different limitations of this validation approach.

In today’s article we shall aim at finding the solution to one of the limitations we discussed in the previous post, i.e. Previous inputs from user get lost when the component rerenders.

So in order to get around this we can use SessionStorage to store the input values. With SessionStorage, web applications can store data locally within the user’s browser per session. To know more, follow this link:

This idea was suggested to me by Alex Edelstein, Product Manager of Flows, Salesforce. Unfortunately, this doesn’t provide us a complete solution to our problem which we will address towards the end of this article. Right now, let’s see how we can achieve this.


The idea is to temporarily store the values input by user into the the SessionStorage of the web browser everytime a user hits the Next button, and initialize the component input fields with those values when component validation fails.

Let’s flow!

To keep things simple, I created a simple flow screen lightning component with two inputs.


<aura:component implements="lightning:availableForFlowScreens" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
    <aura:attribute name="validate" type="Aura.Action" 
                    description="Custom validation function to run when the flow is navigated to the next screen. The function must evaluate the component and return values for isValid and errorMessage."
    <aura:attribute name="firstName" type="String"/>
    <aura:attribute name="lastName" type="String"/>

    <lightning:input type="text" label="First Name" value="{!v.firstName}" required="true"/>
    <lightning:input type="text" label="Last Name" value="{!v.lastName}" required="true"/>


    init: function(component, event, helper) {
        //Get inputs when validation failed
        // Set the validate attribute to a function that includes validation logic
        component.set('v.validate', function() {
            var isFirstNameValid=false; //Check if input is valid
            var firstName = component.get('v.firstName');
            if(firstName && firstName.length>0)
                isFirstNameValid = true;
            var isLastNameValid=false; //Check if input is valid
            var lastName = component.get('v.lastName');
            if(lastName && lastName.length>0) 
                isLastNameValid = true;
            //When all fields are valid, remove the values from sessionStorage
            if(isFirstNameValid && isLastNameValid){
                    sessionStorage.removeItem('validationfailed'); sessionStorage.removeItem('firstName');
                    sessionStorage.removeItem('lastName'); sessionStorage.removeItem('isFirstNameValid');
                return { isValid: true };
            // When validation fails
                    //Set the input values into sessionStorage
                return {isValid: false, 
                        //Validation Error message 
                        errorMessage: 'Please fill all the required fields on the page.' 

Time for some action!

But there’s a catch!

As we discussed in the beginning, this workaround doesn’t really provide us the complete solution. Why? Because the SessionStorage is browser dependent. So if a user is running the flow via Mobile app, this solution will not work. So the hunt for a better solution is still on!
Now if you are wondering, what about the second limitation which we discussed in my previous post. We shall try to address it in the part of this series. 🙂

I dropped an idea on the community for the same. Please upvote to make our life easy!

Thank you for being an awesome reader! Subscribe to this blog for all the updates delivered straight to your inbox. 🙂

One thought on “Retain input values while validating custom Flow Screen Lightning components – Part 2

  1. Pingback: Adding Validation Errors on Individual Inputs in custom Flow Screen Components – Part 3 |  forcePanda

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.