by
KK » Wed Aug 10, 2022 2:11 am
Priya,
Two issues with your code -
1. Syntax error which will result in invalid JSON (ending quote at the wrong place and an extra '/>')-
- Code: Select all
<cms:escape_json><cms:get "frm_my_text2 />" /></cms:escape_json>,
It should have been -
- Code: Select all
<cms:escape_json><cms:get "frm_my_text2" /></cms:escape_json>,
That done, the code will
still not save data to the newly created page which leads to the second issue -
2. Since this is a new page, the original code which creates an array named my_data will actually create a simple variable because <cms:show_repeatable 'my_repeatable' as_json='1' /> will return nothing (it is a new page and this field does not exist yet) -
- Code: Select all
<cms:capture into='my_data' is_json='1'>
<cms:show_repeatable 'my_repeatable' as_json='1' />
</cms:capture >
And so the ensuing code that tries to set a new row into the expected array will also fail (because there is no array to be found).
To fix this, instead of the code above, we'll need to explicitly create an empty array before filling it with submitted data as follows -
- Code: Select all
<cms:set my_data='[]' is_json='1' />
This is how the relevant portion of your modified form should look now (please notice the
scope='parent' added to <cms:capture> for working with the amended code) -
- Code: Select all
<cms:set my_data='[]' is_json='1' />
<cms:capture into='my_data.' is_json='1' scope='parent'>
{
"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 >
And now your form should work as expected.
As documentation for others, the full code for setting repeatable-region while creating a new page is as follows -
- Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template clonable="1">
<cms:repeatable name='my_repeatable' order='-2'>
<cms:editable name='my_text' type='textarea' />
<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'
enctype='multipart/form-data'
method='post'
anchor='0'
>
<cms:if k_success >
<cms:set my_data='[]' is_json='1' />
<cms:capture into='my_data.' is_json='1' scope='parent'>
{
"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='textarea' /><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>
<?php COUCH::invoke(); ?>
Hope this helps.