Problems, need help? Have a tip or advice? Post it here.
10 posts Page 1 of 1
I've got a databound form with an additional field which is processed before adding to the database (it's a UK postcode which I convert to a latitude/longitude using a public API, then store the location instead of the postcode).

If the processing returns a zero for the latitude, then there is something wrong - probably an incorrect postcode, or a very new one which has not been added to the postcoding API yet.

If this happens, how can I raise an error which will be handled by the usual
Code: Select all
<cms:each k_error >
        <br><cms:show item />
</cms:each>

section of the standard Couch form-processing code?
I'll need to know how that processing code is integrated with the field.
Could you please share the code?
Hi, @KK

Here's the full code for the form.

Actually, what I should be doing is checking the returned json for ' "status":404 ' - if that is found, then an error 'Postcode not found' should be raised. Checking for a zero in the CouchCMS variable 'returnedpiclat' is lazier but also works!

Code: Select all
        <cms:set submit_success="<cms:get_flash 'submit_success' />" />
        <cms:if submit_success >
            <h4>Success: Your picture has been submitted.</h4>
        </cms:if>
        <cms:form
            masterpage=k_template_name
            mode='create'
            enctype='multipart/form-data'
            method='post'
            anchor='0'
        >
            <cms:if k_success >
                <cms:php>
                    global $CTX;
                    $inputPostCode = $CTX->get('frm_picpostcode');
                    // - get lat & long from postcode & inject into Couch form values
                    $json = file_get_contents("https://api.postcodes.io/postcodes/".$inputPostCode);
                    $details = json_decode($json, true);
                    $returnedLat = $details['result']['latitude'];
                    $returnedLong = $details['result']['longitude'];
                    $CTX->set('returnedpiclat',$returnedLat);
                    $CTX->set('returnedpiclong',$returnedLong);
                </cms:php>
                <cms:db_persist_form
                    _invalidate_cache='0'
                    _auto_title='1'
                    pic_user = k_user_id
                    piclong = "<cms:show returnedpiclong />"
                    piclat = "<cms:show returnedpiclat />"
                    k_publish_date='0000-00-00 00:00:00'
                />
                <cms:if k_success >
                    <cms:set_flash name='submit_success' value='1' />
                    <cms:redirect '/pictures.php?u=1' />
                </cms:if>
            </cms:if>
            <cms:if k_error >
            <div class="error">
                <cms:each k_error >
                    <br><cms:show item />
                </cms:each>
            </div>
            </cms:if>

            <label>Diocese</label>
            <cms:input name="k_page_folder_id" type="bound" />

            <label>Group</label>
            <cms:input name="picgroup" type="bound" />

            <label>Description</label>
            <cms:input name="piccomment" type="bound" />

            <label>Postcode</label>
            <cms:input name="picpostcode" type="text" />

            <label>Upload your Picture</label>
            <cms:input name="picimage" type="bound" />

            <button type="submit">Submit Picture</button>
           
        </cms:form>
Thanks.

I haven't tested but the following modification to your code should do the trick -
Code: Select all
<cms:if k_success >
    <cms:php>
        global $CTX;
        $inputPostCode = $CTX->get('frm_picpostcode');
        // - get lat & long from postcode & inject into Couch form values
        $json = file_get_contents("https://api.postcodes.io/postcodes/".$inputPostCode);
        $details = json_decode($json, true);
        $returnedLat = $details['result']['latitude'];
        $returnedLong = $details['result']['longitude'];
       
        // do error checking here and cancel success if failed e.g.
        if( !$returnedLat || !$returnedLong ){
            $CTX->set( 'k_success', '' );
            $CTX->set( 'k_error', 'Something is wrong - probably an incorrect postcode' );
        }
        else{
            $CTX->set('returnedpiclat',$returnedLat);
            $CTX->set('returnedpiclong',$returnedLong);
        }
    </cms:php>
   
    <cms:if k_success >
        <cms:db_persist_form
            _invalidate_cache='0'
            _auto_title='1'
            pic_user = k_user_id
            piclong = "<cms:show returnedpiclong />"
            piclat = "<cms:show returnedpiclat />"
            k_publish_date='0000-00-00 00:00:00'
        />
        <cms:if k_success >
            <cms:set_flash name='submit_success' value='1' />
            <cms:redirect '/pictures.php?u=1' />
        </cms:if>
    </cms:if>
</cms:if>

<cms:if k_error >
    <div class="error">
        <cms:each k_error >
            <br><cms:show item />
        </cms:each>
    </div>
</cms:if>

As you can see, we are using on more layer of 'k_success' before committing to <cms:db_persist_form>.
Does this help? Please let me know.
Thanks, @KK - that works nicely!
I am glad it helped :)
Alongside the k_error you may set an k_error_picpostcode and fill it with the server's reply. That could give you / the visitor a hint in the form, as we often do with other malformed inputs.

@KK, I do not remember an example posted about fetching data from multiple sources (or even one) while processing the form (PayPal processor only sends data). Perhaps, it is up to you to show how it is to be done properly, asynchronous and buffered. Form submission can stall due to inadequate handling of such matter. I am sure everyone would bow in admiration :).
@trendoman
done properly, asynchronous and buffered

Async operations, sadly, are not PHP's forte (that distinction goes to server-side JS e.g. NodeJS).
Honestly, I don't think I have an answer to your query.
@KK, I did not mention PHP.
@trendoman,
I did not mention PHP

Apologies Anton but I don't think I quite understood the query then -
Couch code will necessarily use PHP; if you have something different in mind, please let me know in detail.

Thanks.
10 posts Page 1 of 1
cron