Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
Good Morning!

I am trying to implement Bootstrap Validation.

I have done the following to apply it on a <cms:input>
Code: Select all
Editable:
<cms:editable name="full_name" label="Full Name" type="text" required="1" />

Input:
<cms:input name="full_name" id="full_name" type="bound" class="form-control<cms:if k_error_full_name> is-invalid</cms:if>" placeholder="Full Name" />
<label for="full_name">
    Full Name <span class="required">*</span>
</label>
<cms:if k_error_full_name>
    <p id="full_name_error" class="gxcpl-error gxcpl-body-2" style="display: block;">
        <em>Required</em>
    </p>
</cms:if>

the above code works perfectly fine.

But when I try yo access the "k_page_title" field, it does not. work.
Code: Select all
Sample code:

Editable: there is no editable defined. The template is clonable and hence the k_page_title field is available.

Input:
<cms:input name="k_page_title" id="k_page_title" type="bound" class="form-control<cms:if k_error_k_page_title> is-invalid</cms:if>" placeholder="benefit-type" />
<label for="k_page_title">
    Benefit Type <span class="required">*</span>
</label>
<cms:if k_error_k_page_title>
    <p id="k_page_title_error" class="gxcpl-error gxcpl-body-2" style="display: block;">
        <em>Required</em>
    </p>
</cms:if>

How can the validation be used on a system variable field?

Regards,
GXCPL (CTR)
Image
where innovation meets technology
Hi,

Field 'k_page_title' is *not* a required field ('k_page_name' is) and hence no error is getting triggered in the form for your code to take effect.

One way of making it a required field would be placing the following code in your addons/kfunctions.php file (make sure to set the correct template name in it) -
Code: Select all
// make 'k_page_title' a required field
$FUNCS->add_event_listener( 'page_prevalidate',  function(&$fields, &$pg){
    if( $pg->tpl_name == 'xyz.php' ){ // set the correct template
        $pg->_fields['k_page_title']->required = 1;
    }
});

Now 'k_error_k_page_title' variable should get set if the title is left empty and your code should work as you expect it to.

Hope this helps. Do let me know.
Hope this helps. Do let me know.

Yes sir this works. But there is one thing I forgot to mention. I have more than one template where in I need to apply the validation to the k_page_title. I tried something like this:
Code: Select all
$FUNCS->add_event_listener( 'page_prevalidate',  function(&$fields, &$pg){
    if( $pg->tpl_name == 'news/index.php' ){ // set the correct template
        $pg->_fields['k_page_title']->required = 1;
    }
    else if( $pg->tpl_name == 'master-entries/benefit-type.php' ){ // set the correct template
        $pg->_fields['k_page_title']->required = 1;
    }
});

and also:
Code: Select all
$FUNCS->add_event_listener( 'page_prevalidate',  function(&$fields, &$pg){
    if( $pg->tpl_name == 'news/index.php' || $pg->tpl_name == 'master-entries/benefit-type.php' ){ // set the correct template
        $pg->_fields['k_page_title']->required = 1;
    }
});

and also:
Code: Select all
$FUNCS->add_event_listener( 'page_prevalidate',  function(&$fields, &$pg){
    $templates = array('news/index.php', 'master-entries/benefit-type.php');

    foreach ($templates as $template) {
        if( $pg->tpl_name == $template ){
            $pg->_fields['k_page_title']->required = 1;
            break; // Exit the loop once the template is found
        }
    }
});


But it doesnot work. How can I have the validation set for multiple templates?

Regards,
GXCPL (CTR)
Image
where innovation meets technology
3 posts Page 1 of 1