Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
7 posts Page 1 of 1
For use-cases where a page should not be allowed to get saved without a folder being selected, following solution should help -

1. Add the following editable region to the template in question.
Code: Select all
<cms:editable type='hidden' name='dummy' validator='my_check_folders' order='-1' >1</cms:editable>

As can be seen, it is a dummy field with no purpose other than to trigger the validation routine in point 2 below)

2. Add the following validator function in 'couch/addons/kfunctions.php' file (if the file does not exist, rename the 'kfunctions.example.php' to 'kfunctions.php') -
Code: Select all
// validator for core folders dropdown (set on a dummy hidden field)
function my_check_folders( $field, $args ){
    $f = $field->page->_fields['k_page_folder_id'];
    $fid = $f->get_data();
    if( $fid=='-1' ){
        $f->err_msg='Please select a folder'; // set error on folder field
        return KFuncs::raise_error( '' ); // returning an error to fail page save
    }
}

And now if no folder is selected while saving, the page should throw an error.
Hope this helps,

This is a very useful function for me!

I have a site where the main menu list are all the parent folders (unchangeable by anyone except the super-admin) and pages can ONLY be set on the subfolders.

Can this code be altered to choose ONLY subfolders?

@scratz
Can this code be altered to choose ONLY subfolders?

Sure :) Here you go -

Code: Select all
// validator for core folders dropdown (set on a dummy hidden field)
function my_check_folders( $field, $args ){
    $f = $field->page->_fields['k_page_folder_id'];
    $fid = $f->get_data();
    if( $fid=='-1' ){
        $f->err_msg='Please select a folder'; // set error on folder field
        return KFuncs::raise_error( '' ); // returning an error to fail page save
    }

    // further check if the selected folder is not a top-level one
    $pg = $field->page;
    if( $pg instanceof KWebpage ){
        $folder = $pg->folders->find_by_id( $fid );
        if( $folder->pid == '-1' ){ // top-level folder has a parent-id of '-1'
            $f->err_msg='Top-level folder cannot be selected'; // set error on folder field
            return KFuncs::raise_error( '' );
        }
    }
}

Do let me know if this helps.

Thank you @KK. I will test this in a few days and post the results here. I'm assuming this restriction is for the back-end Admin pages. Does this restriction also translate to front-end DBFs?


In the meantime, is the only way to donate to you (couch) to buy a license for a website?

Does this restriction also translate to front-end DBFs?

As per my understanding, the distinction between front-end DBF and the admin-panel form is now almost non-existent (admin-panel also uses DBFs) so this folder restriction should hold good on the frontend too (at least with <cms:db_persist_form>. I haven't tested this though - please do that and let me know.

is the only way to donate to you (couch) to buy a license for a website?

If you feel like doing that, it'd be much appreciated :)
Several of our users buy license multiple times for the same domain to show their support for Couch.
You may also use the following link for a sundry amount -
https://www.couchcms.com/payment.html

Thanks.

This function works perfectly as described for the back end.
As far as a very simple front-end form, the submission shows as a successful creation, but the page doesn't actually create on the back end. I may need to look into formatting the error to show up, but I'm not sure why the form assumes success

Code: Select all
<h2>Create Page</h2>
<div class="container-xxl py-5">
        <div class="container">
            <div class="row mb-5">               
                <div class="col-lg">

                <cms:set submit_success="<cms:get_flash 'submit_success' />" />
                <cms:if submit_success >
                    <h4>Success: Your Page has been created.</h4>
                </cms:if>



                    <cms:form
                        masterpage=k_template_name
                        mode='create'
                        enctype='multipart/form-data'
                        method='post'
                        anchor='0'
                    >
                        <cms:if k_success>
                     <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>
                        Regular Dump:<cms:dump />
                        <div class="row g-3">
                       
                  <div class="col-12">
                            <div class="form-floating">
                     <cms:input name='k_page_title' type='bound' class="form-control" id="title"  />
                                <label for="k_page_title">Page Title</label>
                            </div>
                        </div>
                  <div class="col-12">
                            <div class="form-floating">
                                <cms:input name='hd_image' type="bound" class="form-control" id="hd_image"  />
                                <label for="f_hd_image">Main Image for Page</label>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="form-floating">
                                <cms:input name='img_alt' type="bound" class="form-control" id="img_alt"  />
                                <label for="f_img_alt">Alt Description for Image</label>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="form-floating">
                                <cms:input name='article' type='bound' class="form-control" id="article" />
                                <label for="f_article">Page Copy</label>
                            </div>
                        </div>
                        <div class="col-6">
                            <button class="btn btn-primary py-3 px-5" type="submit">Save Page</button>
                     <cms:input name='dummy' type='bound' />
                        </div>
                    </div>
                </cms:form>
            </div>
        </div>
    </div>
</div>



I have happily bought a license for another of my domains. I realize you and Cheesy and Anton have helped me out so much in the past several years. Thank you for all yall do!!

@scratz,
First of all - thank you very much for getting the license and supporting Couch :)

Coming to the form, you mentioned -
but I'm not sure why the form assumes success

That is because the <cms:form>, per se, is responsible only for regular (i.e. non-bound) inputs. When it flags success, we then invoke <cms:db_persist_form /> that is responsible for all 'bound' inputs. It is only when <cms:db_persist_form /> also flags success that we can be certain that a page has been created/modified in the backend.

Your form code was missing the test for the 'second' success flag mentioned above.
Following is a working and tested form that should help you in rectifying your code (it works well, as expected, with the folder check you asked for).

Code: Select all
<h2>Create Page</h2>

<cms:set submit_success="<cms:get_flash 'submit_success' />" />
<cms:if submit_success >
    <h4>Success: Your Page has been created.</h4>
</cms:if>

<cms:form
    masterpage=k_template_name
    mode='create'
    enctype='multipart/form-data'
    method='post'
    anchor='0'
>
    <cms:if k_success>
        <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>

    <cms:if k_error>
        <cms:each k_error >
            <cms:show item /><br>
        </cms:each>
    </cms:if>

    <cms:input name='k_page_title' type='bound'  /><br>
    <cms:input name='k_page_folder_id' type='bound'  /><br>
    <cms:input name='dummy' type='bound' />

    <button type="submit">Save Page</button>
</cms:form>

Hope this helps.
7 posts Page 1 of 1