Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
51 posts Page 3 of 6
Previous 1, 2, 3, 4, 5, 6 Next
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.
@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"}]' ).

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

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
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.

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?

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
So far my successful hack looks ugly -
Code: Select all
$_POST['_f_params_sortorder'] = '';

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
@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.
Developed full solution - drop a json into a textarea and it will overwrite the repeatable region.
Working demo screenshot:

demo-repeatable-from-json-admin-panel.png
demo-repeatable-from-json-admin-panel.png (12.22 KiB) Viewed 937 times

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
Good day everyone,

@KK, from my understanding this allows you to add one repeatable row at a time, but for my use-case, I am dinamically generating a form that the user has to fill in and publish as multiple rows in the repeatable regions.

Say I have a repeatable region that has as fields:
Code: Select all
------Name------Phone number------Email address------


And my form generates " x " of these. How could i publish these as " x " rows at the same time?

Is this possible?
@aleksandru

Hi.

From the sample code in the original post, you'll see that we begin with an array 'my_data' that contains existing rows of the repeatable-region and then we add one single row to that array as follows -
Code: Select all
<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 >

If we were to repeat the code above once more, it will add one more row to the array - the data would be the same as the previous row but that is because we used the same values (frm_my_text2 etc.); if the values were different, the second row will have different data.

And my form generates " x " of these. How could i publish these as " x " rows at the same time?

I think you should be able to see now that for 'x' times, you can repeat <cms:capture> block 'x' times (each time with the relevant set of fields) and you should be able to add 'x' number of new rows to the repeatable-region.

Hope this helps.
-----------SOLVED-----------



KK wrote: @aleksandru
Code: Select all
<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 >


Hope this helps.



Thank you @KK,

I made all the needed changes but I am facing a weird error that I need to debug step by step, the form is giving me an error of
ERROR: Tag "form" - Page not found


I am using the main template page to edit a clone of itself accodring to some selections and the way I do this in the form is:
Code: Select all
<cms:set q_pn = "<cms:gpc 'q_pn' />" /> <!--The page name is taken from a query string of previous selections-->
<cms:set page_to_edit="<cms:pages masterpage=k_template_name page_name=q_pn><cms:show k_page_id />" />
<!--The page id is selected and displayed correctly-->

</cms:pages>" />
    <cms:form
        masterpage=k_template_name
        mode='edit'
        page_id=page_to_edit
        enctype='multipart/form-data'
        method='post'
        anchor='0'
        >
...


Any ideas as to why that might happen?

In the meanwhile I will simplify it step by step and use ignore to debug and see where the issue is.
Previous 1, 2, 3, 4, 5, 6 Next
51 posts Page 3 of 6