How to reset the form values when clear_on_submit is set to False

I’m in a situation that I’m using clear_on_submit=False. Because some of my form widgets are required. So I’m putting that option to false. But I want to reset the value of widgets to empty if the user give all correct values and then click submit. But somebody suggested me to use st.session_state for that. I’m confused with that. Could anyone please solve me problem??
Here is the code block

def create():
    st.header('Fill the from!!')

    with st.form('Student From',clear_on_submit=False):
        roll_no=st.text_input('Enter your roll_no').lower()
        name=st.text_input('Enter your name').lower()
        branch=st.text_input('Enter your branch').lower()
        location=st.text_input('Enter your loaction').lower()
        phone_no=st.text_input('Enter your phone')
        submitted=st.form_submit_button('Submit')
        
        if submitted:
            if not roll_no:
                st.warning('Please fill the roll number field !!')
            elif len(phone_no)!=10:
                st.warning('Phone number length should be 10',icon='🚨')
            else:
                insert(roll_no,name,branch,location,phone_no)

Hi @Guna_Sekhar_Venkata

For your use case, I think using clear_on_submit=True would be suitable as you’d like all input widgets to reset upon clicking on the Submit button from within st.form.

Session state would be applicable when you want to retain the widget state across app re-runs which is not the use case that you would like.

Hope this helps!

Yes, but I want to check the user is giving roll number or not and also the len of the phone number. I’m doing those using the Boolean value which I’m assigning to the variable submitted. According to you, if im setting that value to True means I’m unable to check those conditions especially in the forms. !!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.