Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
27 posts Page 3 of 3
Can I change mode = 'create' instead of mode = 'edit'?
Code: Select all
    <cms:template clonable='1'>
        <cms:repeatable name='my_repeatable'>
            <cms:editable name='my_text' type='text' />
            <cms:editable name='my_checkbox' type='checkbox' opt_values='Newspaper || Website || Phone Book' />
            <cms:editable name='my_dropdown' type='dropdown' opt_values='Make a selection=- | Yes=2 | No=1' />
        </cms:repeatable>
    </cms:template>


    <cms:set submit_success="<cms:get_flash 'submit_success' />" />
    <cms:if submit_success >
        <h4>Saved.</h4>
    </cms:if>

    <cms:form
        masterpage=k_template_name
        mode='create'
        pafe_id=k_page_id
        enctype='multipart/form-data'
        method='post'
        anchor='0'
        >

        <cms:if k_success >
            <cms:capture into='my_data' is_json='1'>
                <cms:show_repeatable 'my_repeatable' as_json='1' />
            </cms:capture >
           
            <cms:capture into='my_data.' is_json='1'>
               {
                  "my_text" : <cms:escape_json><cms:show frm_my_text2 /></cms:escape_json>,
                  "my_checkbox" : <cms:escape_json><cms:show frm_my_checkbox2 /></cms:escape_json>,
                  "my_dropdown" : <cms:escape_json><cms:show frm_my_dropdown2 /></cms:escape_json>
               }
            </cms:capture >

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

            <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 >
            <div class="error">
                <cms:each k_error >
                    <br><cms:show item />
                </cms:each>
            </div>
        </cms:if>

        <!--
            bound inputs can be placed here
        -->
        <cms:input name='my_text2' type='text' /><br>
        <cms:input name='my_checkbox2' type='checkbox' opt_values='Newspaper || Website || Phone Book' /><br>
        <cms:input name='my_dropdown2' type='dropdown' opt_values='Make a selection=- | Yes=2 | No=1' /><br>

        <cms:input type='submit' name='submit' value='Submit' />
    </cms:form>   
Can I change mode = 'create' instead of mode = 'edit'?

I don't see any problem with that.
--
Thank you! [̲̅$̲̅(̲̅ ͡° ͜ʖ ͡°̲̅)̲̅$̲̅]
HEPA [Help Pages Addon]
@KK

I am exploring the path of setting RR's data dynamically from backend. Example use-case is occasionally pasting a huge json into a textarea field and voila - repeatable region is set without a ton of work.

Tag cms:persist is not reliable for this case - it saves only the first row, it demands only a variable name (rregion = my_data) and doesn't accept a json-encoded string as region's data (doesn't work as rregion = '[{"field":"data"}]' ).

I am using the following code for the demo template which illustrates the problems -
Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template clonable='1'>
    <cms:repeatable name='my_repeatable'>
        <cms:editable name='my_text' type='text' />
        <cms:editable name='my_text2' type='text' />
        <cms:editable name='my_checkbox' type='checkbox' opt_values='=1' />
    </cms:repeatable>
    <cms:editable type='textarea' name='log' label='Log' />
</cms:template>


<cms:config_form_view>
    <cms:persist
        dummy = "<cms:set my_persist_data = '[{"my_text":"578","my_text2":"","my_checkbox":"1"},{"my_text":"99","my_text2":"","my_checkbox":"1"}]' is_json='1' />
                 <cms:capture into='my_persist_data_log'>
Var 'my_persist_data' is set: <cms:show my_persist_data as_json='1' />.
Is array? <cms:is_array my_persist_data />.
Array cnt: <cms:arr_count my_persist_data />.
                 </cms:capture>"
        my_repeatable = my_persist_data
        log = my_persist_data_log
    />
</cms:config_form_view>
<?php COUCH::invoke(); ?>


Did I miss something or it is not possible to make cms:persist save repeatable region?

Thanks and regards
@ant
Thank you! [̲̅$̲̅(̲̅ ͡° ͜ʖ ͡°̲̅)̲̅$̲̅]
HEPA [Help Pages Addon]
I even tried a snippet, but it only saves 2 rows if accessed as super-admin from the front-end (<cms:persist> is triggered). However saving a page in the backend only saves the first row.
Code: Select all
<cms:persist dummy = "<cms:embed 'save.html' />"
             params = my_rr />

// Content of save.html -
<cms:set my_rr  = '[{"param" : "width", "value":"123"},{"param" : "width", "value":"555"}]' is_json='1' scope='global'/>



EDIT:
I also tried to no luck creating a variable inside cms:html -
Code: Select all
    <cms:html>
        <cms:set my_rr  = '[{"param" : "width", "value":"123"},{"param" : "width", "value":"555"}]' is_json='1' scope='global'/>
    </cms:html>


The variable is available within cms:persist, but a very strange thing happens - if I manually add another row (total now 3) then repeatable gets overwrited (just like I need) with the values set in the global variable. However if I delete 1 row from the 2 saved rows and save the page - the change persists, single row gets saved and values from the global variable do not overwrite RR's data. Seems like overwriting doesn't happen all the time (as with other editables).

Please, any comments?
Thank you! [̲̅$̲̅(̲̅ ͡° ͜ʖ ͡°̲̅)̲̅$̲̅]
HEPA [Help Pages Addon]
So far my successful hack looks ugly -
Code: Select all
    <cms:persist dummy = "<cms:php>$_POST['_f_params_sortorder'] = '';</cms:php>"
                 params = my_rr
    />
Thank you! [̲̅$̲̅(̲̅ ͡° ͜ʖ ͡°̲̅)̲̅$̲̅]
HEPA [Help Pages Addon]
@trendoman,
Your 'hack' seems to be the correct solution in this situation.

See, if a RR is physically present in a form, it will also have a companion field (if the RR is named 'xxx', the companion will be named '_f_xxx_sortorder') - this field keeps track of the rows order as they are moved up/down or added/deleted.

When the form is submitted, if Couch finds that the companion field is missing it assumes that the RR values were being set programmatically (i.e. the RR was not actually present in the form), like in all the previous examples of this thread, and acts accordingly.

In your case, the RR *is* physically present in the form *and* you are trying to programmatically set the the RR's values.
This is confusing the save routine.

The solution is to remove the companion sort field from the posted values - this will make Couch disregard the physically present RR and use only the programmatically set values.
27 posts Page 3 of 3

Who is online

In total there are 3 users online :: 0 registered, 0 hidden and 3 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 3 guests