Condition for clear_on_submit

How to activate clear_on_submit only when all fields are filled in. Therefore, the form that is being filled out should not be cleaned, but the warning that there is a missing field to be filled out should be preserved.

import streamlit as st

def main():
    st.title("Streamlit Form")

    with st.form(key='myform', clear_on_submit=True):
        firstname = st.text_input("Firstname")
        message = st.text_area("Message")
        submit_button = st.form_submit_button(label='Submit')

        if firstname and message:
            if submit_button:
                st.success("Hello {} you ve send message".format(firstname))
        else:
            if submit_button:
                st.warning("Please fill in all fields before submitting.")

if __name__ == '__main__':
    main()
2 Likes