Problems, need help? Have a tip or advice? Post it here.
5 posts Page 1 of 1
Hi all,
I've been having a problem validating a databound form. I was going to post the code for my form, but I decided first to try running the original databound form tutorial (http://docs.couchcms.com/concepts/databound-forms.html). This is the exact code from the tutorial, and it just won't work properly for me.
The problem is that on the front end, there are no errors being recognised, so the success message is displayed. However, the data is not being entered into the template. So I assume that the required and validator tags are being recognised by Couch, but this is not being translated to the front end.
I think this is the first time I have tried using databound forms in this way on 2.0, is this maybe the problem?
Or am I missing something stupid? (this is often the case, just need someone else to look at it and tell me what is wrong!)

Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Application Form' clonable='1'>
    <cms:editable name='first_name' required='1' type='text' />
    <cms:editable name='last_name' required='1' type='text' />
    <cms:editable name='email' required='1' validator='email' type='text' />
    <cms:editable name='portfolio' validator='url' type='text' />
    <cms:editable name='position' required='1' type='dropdown'
        opt_values=' Please choose=- | Interface Designer | Software Engineer | Systems Administrator | Office Manager'
    />
    <cms:editable name='salary' type='text' search_type='decimal' />
    <cms:editable name='resume' required='1' allowed_ext='pdf, doc, docx' max_size='1024' type='securefile' />
</cms:template>
<!doctype html>
<html>
<head>
<title>Employment Application</title>
    <style>
        body{ width:500px; }
        input[type="text"]{ width:99%; }
        .error{ color:red; }
    </style>
</head>

<body>
    <h3>Employment Application</h3>

    <cms:set submit_success="<cms:get_flash 'submit_success' />" />
    <cms:if submit_success >
        <h4>Success: Your application 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:check_spam email=frm_email />

            <cms:db_persist_form
                _invalidate_cache='0'
                _auto_title='1'
            />

            <cms:set_flash name='submit_success' value='1' />
            <cms:redirect k_page_link />
        </cms:if>

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

        <label>First Name</label>
        <cms:input name="first_name" type="bound" required='1' />
        <br />

        <label>Last Name</label>
        <cms:input name="last_name" type="bound" />
        <br />

        <label>Email Address</label>
        <cms:input name="email" type="bound" />
        <br />

        <label>Portfolio Website</label>
        <cms:input name="portfolio" type="bound" />
        <br />

        <label>Which position are you applying for? </label>
        <cms:input name="position" type="bound" />
        <br />

        <label>Salary Requirements</label>
        <cms:input name="salary" type="bound" />
        <br />

        <label>Attach a Copy of Your Resume</label>
        <cms:input name="resume" type="bound" />
        <p>Word or PDF Documents Only</p>
        <br />

        <label>What colour is a blue apple? (4 characters required)</label>
        <cms:input name="human" type="text" required='1' validator='regex=/^blue$/i' />
        <br />

        <cms:if "<cms:not submit_success />" >
            <button type="submit">Submit Application</button>
        </cms:if>

    </cms:form>

</body>
</html>
<?php COUCH::invoke(); ?>
Hi,

Please modify the existing k_success block slightly to make it as follows -
Code: Select all
<cms:if k_success >

    <cms:check_spam email=frm_email />

    <cms:db_persist_form
        _invalidate_cache='0'
        _auto_title='1'
    />

    <cms:if k_success >
        <cms:set_flash name='submit_success' value='1' />
        <cms:redirect k_page_link />
    </cms:if>
</cms:if>

That should show you the error being encountered.
Let me know if this helps.
Yes, that displays the error messages as expected.
Thanks again KK, great help as always.
Out of interest, what was the problem? Obviously you're calling the if k_success tag again within the original if k_success, but how that change things?

My brain just isn't working this Monday I think!
You are welcome :)

Out of interest, what was the problem? Obviously you're calling the if k_success tag again within the original if k_success, but how that change things?

The first k_success block signifies that the form was successfully submitted.
The second k_success block (we added nested within the first) signifies that the <cms:db_persist_form /> tag successfully saved the page.

Had the <cms:db_persist_form /> failed, we wouldn't have redirected and the common <cms:if k_error > block that follows would display the error(s) encountered.

Things have changed slightly in v2.0 so this distinction now becomes more prominent.
That makes sense. I just wasn't connecting the nested k_success with the db_persist, I was just assuming it was still for the form submission.
Great stuff, thanks again KK!
:D
5 posts Page 1 of 1