Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
I just ran into something weird. On every page I have with forms I have use set/get flash to handle success/errors but the thing is, the first time I submit an empty form to get the error, I only get my inputs to red (I set them to add a class on error to make them red) but the flash message will show only if you press the submit button the second time...I don't get it...


Code: Select all
<!-- Set to Handle Everything -->
                <cms:set locuinta_success="<cms:get_flash 'locuinta_success' />" />
            <cms:set locuinta_error="<cms:get_flash 'locuinta_error' />" />
               
            <cms:if locuinta_success >
                    <div class="contact_successbox">Success</div>
            <cms:else_if locuinta_error />
                   <div class="contact_errorbox">Error</div>
            </cms:if>
            <!-- Set to Handle Everything -->
               
                <!-- EXTENDED FORM -->
               <cms:form id="contactForm" action="" method="POST" anchor="0">
                <!-- Handle Everything -->
               
            <cms:if k_success >
               <cms:set_flash name='locuinta_success' value='1' />
               <cms:redirect k_page_link />
            <cms:else_if k_error />
               <cms:set_flash name='locuinta_error' value='1' />
            </cms:if>
...the rest of the form


Also, simplifying it doesn't work either. Still have to submit twice in order to get the message

Code: Select all
            <!-- Set to Handle Everything -->
               <h3>Flash Message: <cms:get_flash 'flash_msg' /></h3>
            <!-- Set to Handle Everything -->
               
                <!-- EXTENDED FORM -->
               <cms:form id="contactForm" action="" method="POST" anchor="0">
                <!-- Handle Everything -->
               
            <cms:if k_error >
               <cms:set_flash name='flash_msg' value="Your submission failed!" />
            </cms:if>


PS: I have no error in my console log and don't cache my pages (also no PrettyURL)
Great support request. With code samples and understandable language. Thanks!

As to the issue. You have a redirect in your code to handle success. In my opinion you have to put redirect also in case of error. flash works after the page is reloaded, so it can be read. Submitting an errored form does not somehow let couch know that page is reloaded. Experienced it myself.
Add redirect there and try again.
Code: Select all
<cms:if k_success >
               <cms:set_flash name='locuinta_success' value='1' />
               <cms:redirect k_page_link />
            <cms:else_if k_error />
               <cms:set_flash name='locuinta_error' value='1' />
<cms:redirect k_page_link />
            </cms:if>
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
trendoman wrote: Great support request. With code samples and understandable language. Thanks!

As to the issue. You have a redirect in your code to handle success. In my opinion you have to put redirect also in case of error. flash works after the page is reloaded, so it can be read. Submitting an errored form does not somehow let couch know that page is reloaded. Experienced it myself.
Add redirect there and try again.


Thank you for your answer and the appreciation.

To be honest, I feared that I had to refresh the page in order to get a correct display of the error for two reasons:

When you refresh the page all the input fields already filled by the user will vanish.
My custom if-error-add-class part won't trigger anymore.

On the other hand, I just realized that there is no reason to use flash to handle the error, in fact, considering the two from above, it would be quite silly of me to do so.[This is what happens when you are way to tired and try to find complicated solutions to simple problems]

I simply need to wrap my error div inside the k_error tag and everything will be just fine:



Code: Select all
                <!-- Set to Handle Everything -->
                <cms:set submit_success="<cms:get_flash 'submit_success' />" />
                <cms:if submit_success >
                    <div class="contact_successbox">Success!</div>
                </cms:if>
            <!-- Set to Handle Everything -->
               
                <!-- EXTENDED FORM -->
               <cms:form id="contactForm" action="" method="POST" anchor="0">
                <!-- Handle Everything -->
            <cms:if k_error>
                   <div class="contact_errorbox">Error!</div>
            </cms:if>
            <cms:if k_success >
               <cms:set_flash name='submit_success' value='1' />
               <cms:redirect k_page_link />
            </cms:if>
                <!-- Handle Everything -->
I agree with you @Alin.

Refreshing the page is done only after a successful submission to deliberately obliterate all submitted values (as otherwise there is a risk of double submission if the user happens to refresh the page).

With an error condition we'd actually like to preserve all submitted values otherwise the hapless user will have to fill out all fields again.

Directly showing the error message is the correct method.

I'd just like to add that sometimes, the HTML markup dictates that the error message be shown outside the FORM.
In such cases we can use <cms:capture> to buffer output of the form and the error message separately and then show them in any desired sequence.
4 posts Page 1 of 1