Problems, need help? Have a tip or advice? Post it here.
17 posts Page 2 of 2

It does work with databound-forms but the validation process in DBF is a little different from regular forms - in DBF, it is a two-step process; first all non-bound inputs are validated and only after they all pass validation do the bound inputs get validated.

So, if the validation in question is for a normal input, if it fails validation it would be reported first (without any bound fields being checked). Once this succeeds, the core will validate the bound fields and show their errors, if any.

Does this explain the perceived problem?

KK, thanks, kind of... I will send you a PM.

@blutbaden, let us use a revised solution that does not use the addition hidden field for validation.

For giving others coming across this thread later, the context pertains to these two editable regions -
Code: Select all
<cms:editable name='contact_reason' label='Reason for contact' type='dropdown' opt_values="Make a selection=- | Order | Service | Customer Service | Other" order='3' required='1' />

<cms:editable name='other_reason' label='Other reason' type='text' order='4' />

What is required is that if the 'Other' option is selected from the first editable region above, then the second region that follows ('other_reason') cannot be allowed to be left empty.

As a solution we can attach a custom validator to the first field itself that does the required checks.
The modified definition of the field now becomes (the only change being the added validator) -
Code: Select all
<cms:editable name='contact_reason' label='Reason for contact' type='dropdown' opt_values="Make a selection=- | Order | Service | Customer Service | Other" order='3' required='1' validator='req_on_submit' />

Next place the code for the validator used above in kfunctions.php file -
Code: Select all
// Custom validator for form field (blutbaden)
function req_on_submit( $field, $args ){

    // check if 'other' option selected..
    $val = trim( $field->get_data() );
    if( $val=='Other' ){

        // check that the 'other_reason' textbox is not left empty then ..
        $f = $field->page->_fields['other_reason'];
        if( !is_null($f) ){
            $val = trim( $f->get_data() );
            if( !strlen($val) ){
                return KFuncs::raise_error( "'Other reason' cannot be left empty" );
            }
        }
    }
}

And that should be it.

Hope it helps.

This is much nicer and worked brilliantly!

I love how different fields can now easily have custom validators.

TYVM KK!

Re:

@KK - Putting the validators to use on other websites, great stuff!

Ran into small issue, could use some help.

The function raises a text error, which returns in with all the other errors. This website does not use text errors and just adds a class to the field to change it's appearance and I accomplish this with something like:

Code: Select all
class="field-box<cms:if k_error_other_reason> error</cms:if>"


I did try that, with no luck so assumed it was just not being set, so I tried to add to the function:

Code: Select all
$CTX->set('k_error_other_reason', '1', 'global');


prior to the return of raise_error.

Code: Select all
// Custom validator for form field (blutbaden)
function req_on_submit( $field, $args ){
    global $CTX;
    $CTX->set('k_error_other_reason', '', 'global'); // clear previous error sets
    // check if 'other' option selected..
    $val = trim( $field->get_data() );
    if( $val=='Other' ){

        // check that the 'other_reason' textbox is not left empty then ..
        $f = $field->page->_fields['other_reason'];
        if( !is_null($f) ){
            $val = trim( $f->get_data() );
            if( !strlen($val) ){
                $CTX->set('k_error_other_reason', '1', 'global');
                return KFuncs::raise_error( "'Other reason' cannot be left empty" );
            }
        }
    }
}


When I add:

Code: Select all
<cms:if k_error_other_reason> error</cms:if>


To the html anywhere, I get error displayed and it seems to function.

When I add it to the bound:

Code: Select all
<cms:input type="bound" name="other_reason" class="field-box<cms:if k_error_other_reason> error</cms:if>" id="other_reason" />


Here is the editable for the other_reason:

Code: Select all
<cms:editable name='other_reason' label='Other reason' type='text' />


It does not add the error class. This was my attempt to try to get it working, am I on the right path?

@Blutbaden, problem is that the error is actually being set on the 'contact_reason' field and not the 'other_reason' (which you are trying to target).

Anyway, let us make on small amendment to our code in kfunctions -
function req_on_submit( $field, $args ){

// check if 'other' option selected..
$val = trim( $field->get_data() );
if( $val=='Other' ){

// check that the 'other_reason' textbox is not left empty then ..
$f = $field->page->_fields['other_reason'];
if( !is_null($f) ){
$val = trim( $f->get_data() );
if( !strlen($val) ){
//return KFuncs::raise_error( "'Other reason' cannot be left empty" );

$f->required=1;
}
}
}
}

The code above will make the second field 'required' if circumstances demand it - this way now the second field will throw the error itself.

Please note that for this code to work, the 'contact_reason' field (i.e the select dropdown) should appear in the admin-panel before the 'other_reason' so as to be able to tweak the second field's 'required' parameter before it is validated (which is how the two fields are defined in the test template we are using so it shouldn't be problem).

And now your code should work -
Code: Select all
<cms:input type="bound" name="other_reason" class="field-box<cms:if k_error_other_reason> error</cms:if>" />

Hope it helps.

Nice! TY!
17 posts Page 2 of 2

Who is online

In total there are 4 users online :: 1 registered, 0 hidden and 3 guests
(based on users active over the past 5 minutes)

Users browsing this forum: Exabot [Bot] and 3 guests