Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
52 posts Page 5 of 6
Previous 1, 2, 3, 4, 5, 6 Next
Block k_success will not have any idea how many 'rows' were added to the form constructor before submitting the form. Your best bet is to send along with the form a hidden input with the number of new rows. So, if the form user adds 5 rows, then this hidden value should allow you to create necessary json with that amount of rows. One other option is to iterate through the $_POST values to see which number is the last one, e.g. field_0, field_1 should give you the last number '1'. This is a not recommended way.

I would add to my previous tip about logging another written-in-blood tip: always work with the simplest code possible to verify the logic works. Create a separate form with only 1-2 inputs and debug stuff on this small subset. Catch a Hot Potato Form as it allows to submit the form without page reloading, instantly showing the k_success block values (copy-pasting it "as is" will immediately work).
Actually, I am trying a little different approach right now.
I am removing the k_success from the main loop and adding a loop inside the k_success to run the same amount of times as my field count. Setting another variable that i increment in that loop and using that for the numbering is promising I believe.

Here is the untested code:
Code: Select all
    <cms:repeat count='2' >
         <cms:incr field_count '1' />
              <tr>
                <td class="tg-0lax"><cms:show k_page_title/><cms:show field_count /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'nominal_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'um_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'new_condition_upper_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'new_condition_lower_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'used_condition_upper_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'used_condition_lower_' field_count />" type='text' /></td>
              </tr>
             
    </cms:repeat>
   
          <cms:set succeess_count='0' />
            <cms:if k_success >
             <cms:repeat count=field_count >
                   <cms:incr succeess_count '1' />
                    <cms:capture into='my_data.' is_json='1'>
                       {
                          "field" : <cms:escape_json><cms:show k_page_title /></cms:escape_json>,
                          "nominal" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'nominal_' succeess_count />"/>"/></cms:escape_json>,
                          "um" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'um_' succeess_count />"/>"/></cms:escape_json>,
                          "new_condition_upper" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'new_condition_upper_' succeess_count />"/>"/></cms:escape_json>,
                          "new_condition_lower" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'new_condition_lower_' succeess_count />"/>"/></cms:escape_json>,
                          "used_condition_upper" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'used_condition_upper_' succeess_count />"/>"/></cms:escape_json>,
                          "used_condition_lower" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'used_condition_lower_' succeess_count />"/>"/></cms:escape_json>
                       }
                    </cms:capture >
             </cms:repeat>      
            </cms:if>


What do you think about that ?
aleksandru wrote: What do you think about that ?

You said — I am dynamically generating a form and the number of fields is not always the same

With a fixed 'repeat' I can see no problem, because all tags on the page that appear before the k_success block also run upon submission → the variable 'field_count' is the same. But what if your form does not have a fixed number of repeated rows (as planned)?
trendoman wrote:
aleksandru wrote: What do you think about that ?

You said — I am dynamically generating a form and the number of fields is not always the same

With a fixed 'repeat' I can see no problem, because all tags on the page that appear before the k_success block also run upon submission → the variable 'field_count' is the same. But what if your form does not have a fixed number of repeated rows (as planned)?


Well, I was using the fixed 'repeat' to create just two rows of input fields and make it easier to fix/understand.
In the final code, the repeat tag is replaced by:

Code: Select all
    <cms:db_fields masterpage=c_masterpage types='text' limit='1'>
        <cms:if label && desc >
            <cms:if type='text'>
            </cms:if>
        </cms:if>
    </cms:db_fields>


Which looks at every editable field in that template that has a label, a description and is a type='text', as the measurement values are the only ones that are of type text.

The investigation templates are done and have between 69 and 84 measurements into them, therefore I used a repeat tag to loop it only once because I wanted to avoid getting so many. This is a different type of generating it dynamically [or maybe i should have had a better choice for words and just say automatically?] since the form depends on the editable tags of a different template's cloned page.

But still, while looping through the editable's of those page, it generates a row with the first input being the label of that editable tag. And after every loop, I am incrementing the value of field_count.
@KK Sir

For our tests, let use a non-clonable template defining only a single repeatable-region within it with three fields


Sir can you please provide an example for CLONABLE template defining only a single repeatable-region within it with TEXTAREA as one of the editable fields?

I will be grateful.

Regards,
GXCPL (Priya)
Image
where innovation meets technology
Sir can you please provide an example for CLONABLE template defining only a single repeatable-region within it with TEXTAREA as one of the editable fields?

Here you are -
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>



<?php COUCH::invoke(); ?>

Hope it helps.
@KK Sir,

Sir, the RR fields are not saving.

This is my code:
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'
       
        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:get "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: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(); ?>


Sir the page is created but the fields are not saved when I create a new page.

Regards,
GXCPL (Priya)
Image
where innovation meets technology
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.
@KK Sir!

Thanks sir. The create issue is solved.

Regards,
GXCPL (Priya)
Image
where innovation meets technology
You are welcome :)
Previous 1, 2, 3, 4, 5, 6 Next
52 posts Page 5 of 6