Problems, need help? Have a tip or advice? Post it here.
12 posts Page 1 of 2
Code: Select all
<div class="co-12 col-md-4 mb-3">
   <cms:set submit_success="<cms:get_flash 'submit_success' />" />
    <cms:if submit_success >
        <div class="alert alert-success shadow mb-3 alert-dismissible fade show">
          Record created.
          <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
       </div>
    </cms:if>

    <cms:set feed_edit="<cms:get_cookie 'feed_edit' />" />
    <cms:if feed_edit >
        <div class="alert alert-success shadow mb-3 alert-dismissible fade show">
          Record updated.
          <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
       </div>
       <cms:delete_cookie 'feed_edit' />
    </cms:if>

    <cms:set feed_delete="<cms:get_cookie 'feed_delete' />" />
    <cms:if feed_delete >
        <div class="alert alert-success shadow mb-3 alert-dismissible fade show">
          Record deleted.
          <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
       </div>
       <cms:delete_cookie 'feed_delete' />
    </cms:if>

   
    <cms:set submit_error="<cms:get_flash 'submit_error' />" />   
    <cms:if submit_error>
       <div class="alert alert-danger shadow mb-3 alert-dismissible fade show">
           Feed already exists. You can edit or delete the feed but cannot re-create one.
           <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
       </div>
   </cms:if>
   

    <div class="card shadow mb-3">
       <div class="card-body">
          <h5 class="card-title mb-3">
             Add Feed
          </h5>
          

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

             <!-- set the following two variables to match your use-case -->
             <cms:set my_template_name = "cattle-feed/index.php" />
             <cms:set my_page_title = "<cms:show frm_feed_name/> <cms:show frm_feed_weight/>" />

             <!-- 1. convert the title into unique page_name -->
             <cms:php>
                 global $CTX, $FUNCS;
                 $name = $FUNCS->get_clean_url( "<cms:show my_page_title />" );
                 $CTX->set( 'my_page_name', $name );
             </cms:php>
             <!-- 2. try to find a page by the name generated above -->
             <cms:set my_page_id = '' 'global' />
             <cms:pages masterpage=my_template_name page_name="<cms:show my_page_name />" limit='1' show_future_entries='1'>
                 <cms:set my_page_id=k_page_id  'global' />
             </cms:pages>
            
             <!-- 3. if 'my_page_id' is empty at this point, the page does not exist - so safe to create one now -->
             my_page_id::<cms:show my_page_id />
             <cms:if my_page_id=''>
                 <cms:if k_success >
                     <cms:db_persist_form
                         _invalidate_cache='0'
                         _auto_title='0'
                         k_page_title = "<cms:show frm_feed_name /> <cms:show frm_feed_weight />"
                         k_page_name = k_page_title
                     />
                     <cms:if k_success >
                        <cms:set_flash name='submit_success' value='1' />
                        <cms:redirect k_page_link />
                    </cms:if>
                 </cms:if>
              </cms:if>

              <cms:if k_error >
                 <cms:set_flash name='submit_success' value='1' />
                  <div class="error">
                      <cms:each k_error >
                          <br><cms:show item />
                      </cms:each>
                  </div>
              </cms:if>

              <div class="form-floating mb-3">
               <cms:input name="feed_name" id="feed_name" type="bound" class="form-control" placeholder="" />
               <label for="feed_name">Feed *</label>
               <cms:if k_error_feed_name>
                    <small>
                       <span id='feed_name_error' class="gxcpl-error" style="display: inline-block;">
                          <em>Required</em>
                       </span>
                    </small>
                 </cms:if>
            </div>

            <div class="form-floating mb-3">
               <cms:input name="feed_weight" id="feed_weight" type="bound" class="form-control" placeholder="" />
               <label for="feed_weight">Packaging Weight (in kgs) *</label>
               <cms:if k_error_feed_weight>
                    <small>
                       <span id='feed_weight_error' class="gxcpl-error" style="display: inline-block;">
                          <em>Required</em>
                       </span>
                    </small>
                 </cms:if>
            </div>

            <button class="btn gxcpl-btn-blue-a700 gxcpl-button shadow" type="submit">
               <i class="fa fa-plus"></i> Create Feed
            </button>

          </cms:form>
       </div>
    </div>
</div>


The code does not generate any error.
I am trying to generate errors:
1. When the page exists
2. when required fields are blank and form is submitted.

How can this be handled?

Regards,
GenXCoders
Image
where innovation meets technology
@KK Sir,

Good evening.

I am trying to combine the following:
1. Databound Forms
2. Prevent duplicate pages from being created, from CSV Importer Module
3. Accessing k_error_<editable name> for error display.

But no error is displayed.

I went though you post here.

Then we use <cms:pages> with the name to search for the page. If the page is *not* found, we continue with the original logic and use <cms:db_persist> to create a new page.


But I find that even if the "my_page_name" is empty, the "my_page_id" is still not empty.

I actually want to achieve something like:
1. Show "Required" as an error if a required field is left empty
2. Show an error that the page already exists if a new page with the same name is being created (my k_page_title = frm_feed_name frm_feed_weight).

But I end up seeing no error at all. The page is not created if a page with the same name exists. Also, there is no error if the required fields are left blank, but the page is not created in this case too. So basically, the code is doing its work, I am only struggling with the error display. Any help/ guidance would be really appreciable.

Regards,
GenXCoders
Image
where innovation meets technology
It's a bit of PHP but a third field with a custom validator will set the errors natively viewtopic.php?f=4&t=10388#p25464

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
It's a bit of PHP but a third field with a custom validator will set the errors natively viewtopic.php?f=4&t=10388#p25464


Bro Anton, I somewhere feel that there would not be a need of the third field and a custom validator.

I am not understanding why the my_page_id is never blank, except when there are no cloned pages at all?

Code: Select all
<!-- set the following two variables to match your use-case -->
             <cms:set my_template_name = "cattle-feed/index.php" />
             <cms:set my_page_title = "<cms:show frm_feed_name/> <cms:show frm_feed_weight/>" />

             <!-- 1. convert the title into unique page_name -->
             <cms:php>
                 global $CTX, $FUNCS;
                 $name = $FUNCS->get_clean_url( "<cms:show my_page_title />" );
                 $CTX->set( 'my_page_name', $name );
             </cms:php>
             <!-- 2. try to find a page by the name generated above -->
             <cms:set my_page_id = '' 'global' />
             <cms:pages masterpage=my_template_name page_name="<cms:show my_page_name />" limit='1' show_future_entries='1'>
                 <cms:set my_page_id=k_page_id  'global' />
             </cms:pages>
             
             <!-- 3. if 'my_page_id' is empty at this point, the page does not exist - so safe to create one now -->
             my_page_id::<cms:show my_page_id />


I tried displaying all the variables being set. And this is what I get:
my_template_name::cattle-feed/index.php
my_page_title::
my_page_name::
my_page_id::3985


Even after all the fields being empty, the moment the execution reaches this code it sets a value:
Code: Select all
<cms:pages masterpage=my_template_name page_name=my_page_name limit='1' show_future_entries='1'>
                    <cms:set my_page_id=k_page_id  'global' />
                    my_page_id::<cms:show my_page_id /><br>
                </cms:pages>


Any idea about this my_page_id::<cms:show my_page_id /> not being blank?

Regards,
GenXCoders (Aashish)
Image
where innovation meets technology
Well, Couch treats empty values passed to a parameter as a non-set parameter. If you use return_sql='1' in the 'cms:pages' you would see how query is *not* what would be expected.

Can you try rewriting the 'page-searching' block as suggested in telegram chat reply with 'page_exists' tag? I guess 'page_exists' will bulk on empty pagename.. (Parameter for the tag 'page_exists' is masterpage and name as posted in Midware » page_exists) That will confirm it. Therefore add a check for the empty page_name before trying to search for it.

Eventually look for the place where the value of the variable is lost..

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
@KK Sir,

Why does the logic for preventing duplicate pages from being created (of CSV) not work with databound forms? Can you please throw some light on this issue?

Regards,
GenXCoders
Image
where innovation meets technology
Any Help!!!
Image
where innovation meets technology
Let us solve the my_page_id never being blank issue first (will take up raising error for duplicate pages later).

In your code, you have placed all the logic of figuring out the page name and then searching for it *outside* the <cms:if k_success> block - what that means is all that code executes even when the form is *not* submitted. The 'frm_' variables are all empty at this point so your 'my_page_name' is also a blank. As @trendoman mentioned, Couch ignores empty params, so basically your <cms:pages> ignores the name and fetches the first page it finds instead. Hope you can see what was happening.

Please try using the following and let me know if this solves that issue
Code: Select all
<cms:form
  masterpage=k_template_name
  mode='create'
  enctype='multipart/form-data'
  method='post'
  anchor='0'
>

    <cms:if k_success >
         <!-- set the following two variables to match your use-case -->
         <cms:set my_template_name = "cattle-feed/index.php" />
         <cms:set my_page_title = "<cms:show frm_feed_name/> <cms:show frm_feed_weight/>" />

         <!-- 1. convert the title into unique page_name -->
         <cms:php>
             global $CTX, $FUNCS;
             $name = $FUNCS->get_clean_url( "<cms:show my_page_title />" );
             $CTX->set( 'my_page_name', $name );
         </cms:php>
         <!-- 2. try to find a page by the name generated above -->
         <cms:set my_page_id = '' 'global' />
         <cms:pages masterpage=my_template_name page_name="<cms:show my_page_name />" limit='1' show_future_entries='1'>
             <cms:set my_page_id=k_page_id  'global' />
         </cms:pages>
         
         <!-- 3. if 'my_page_id' is empty at this point, the page does not exist - so safe to create one now -->
         my_page_id::<cms:show my_page_id />
         <cms:if my_page_id=''>
                 <cms:db_persist_form
                     _invalidate_cache='0'
                     _auto_title='0'
                     k_page_title = "<cms:show frm_feed_name /> <cms:show frm_feed_weight />"
                     k_page_name = k_page_title
                 />
                 <cms:if k_success >
                    <cms:set_flash name='submit_success' value='1' />
                    <cms:redirect k_page_link />
                </cms:if>
          </cms:if>
      </cms:if>

      <cms:if k_error >
         <cms:set_flash name='submit_success' value='1' />
          <div class="error">
              <cms:each k_error >
                  <br><cms:show item />
              </cms:each>
          </div>
      </cms:if>

      <div class="form-floating mb-3">
       <cms:input name="feed_name" id="feed_name" type="bound" class="form-control" placeholder="" />
       <label for="feed_name">Feed *</label>
       <cms:if k_error_feed_name>
            <small>
               <span id='feed_name_error' class="gxcpl-error" style="display: inline-block;">
                  <em>Required</em>
               </span>
            </small>
         </cms:if>
    </div>

    <div class="form-floating mb-3">
       <cms:input name="feed_weight" id="feed_weight" type="bound" class="form-control" placeholder="" />
       <label for="feed_weight">Packaging Weight (in kgs) *</label>
       <cms:if k_error_feed_weight>
            <small>
               <span id='feed_weight_error' class="gxcpl-error" style="display: inline-block;">
                  <em>Required</em>
               </span>
            </small>
         </cms:if>
    </div>

    <button class="btn gxcpl-btn-blue-a700 gxcpl-button shadow" type="submit">
       <i class="fa fa-plus"></i> Create Feed
    </button>

</cms:form>
Var k_page_name can't be set from k_page_title — this will throw an error.
Code: Select all
k_page_title = "<cms:show frm_feed_name /> <cms:show frm_feed_weight />"
k_page_name = k_page_title

Following snippet will always set my_page_id — it will continue to 'never be blank'.
Code: Select all
<cms:pages masterpage=my_template_name page_name="<cms:show my_page_name />" limit='1' show_future_entries='1'>
     <cms:set my_page_id=k_page_id  'global' />
</cms:pages>

To really get an empty 'my_page_id' flag set, there must be a condition wrapping the snippet above —
Code: Select all
<cms:if my_page_name ne "">
   <cms:pages ...
</cms:if>

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
@trendoman,

I agree with your first point - k_page_title will likely have characters that are not acceptable in the name.
(@genxcoder, you should simply omit k_page_name altogether and Couch will fill it by sanitizing the provided k_page_title.)

As for your second point
Following snippet will always set my_page_id — it will continue to 'never be blank'.

I can't see why that would be the case (assuming page_name has not been set to an empty value, as is being done in OP's code). With a valid 'page_name', if a page by that name is not found, <cms:pages> will not execute its enclosed statements and my_page_id will not be set.

Please correct me if the reasoning is not right.
12 posts Page 1 of 2
cron