Skip Navigation

[Resolved] cred_form_validate does validate a custom field

This thread is resolved. Here is a description of the problem and solution.

Problem: I am trying to validate a custom field with cred_form_validate, but I cannot validate a specific field. Errors never appear in the form.

Solution: Be sure to include the form_messages field in the Form builder so errors can be displayed. Then use the following syntax to validate a custom field:

function fn_cred_form_validate($error_fields, $form_data)
{
    list($fields,$errors)=$error_fields;
  
    if ($form_data['id']==221) {
        if (empty($fields['wpcf-application-tour-number']['value'])) {
            //set error message for my_field
            $errors['application-tour-number']='Please select tour first';
        }
    }
    return array($fields,$errors);
}
add_filter('cred_form_validate','fn_cred_form_validate',10,2);

Notice that the wpcf- prefix is required in the $fields array, but should not be included in the $errors array.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by kelvinL-2 3 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1985065

Dear Sir/Madam,

I have a CRED form

[credform]
		[cred_field field='application-tour-number' force_type='field' class='form-control ' output='bootstrap' urlparam='tour-id']
		[cred_field field='form_messages' class='alert alert-warning']
...

If I empty the field 'application-tour-number', post still can be saved.

function fn_cred_form_validate($error_fields, $form_data)
{
    list($fields,$errors)=$error_fields;

    if ($form_data['id']==221) {
        if (empty($fields['wpcf-application-tour-number']['value'])) {
            $errors['wpcf-application-tour-number']='Please select tour first';
        }
    }
    return array($fields,$errors);
}
add_filter('cred_form_validate','fn_cred_form_validate',10,2);

I did try

        if ($fields['wpcf-application-tour-number']['value']=='') {
            $errors['wpcf-application-tour-number']='Please select tour first';
        }

but no error display and continue to save.

Please advise how problem to my form and script.

#1985815

Try this syntax:

if (empty($fields['wpcf-application-tour-number']['value'])) {
            $errors['application-tour-number']='Please select tour first';
        }

I think the wpcf- prefix is required in $fields but not in $errors.

#1991775

I do try empty() or $errors['application-tour-number'] or $errors['wpcf-application-tour-number'] but not work

function fn_cred_form_validate($error_fields, $form_data)
{
    list($fields,$errors)=$error_fields;

    if ($form_data['id']==221) {
        if (empty($fields['wpcf-application-tour-number']['value'])) {
            //set error message for my_field
            $errors['wpcf-application-tour-number']='Please select tour first';
        }
    }
    return array($fields,$errors);
}
add_filter('cred_form_validate','fn_cred_form_validate',10,2);
#1992207

I just ran a test with a Types number field, and this format is validated as I expected:

    if (empty($fields['wpcf-slug']['value']))
        {
            //set error message for slug field
            $errors['slug']='slug field number is required';
        }

The wpcf- prefix is required for Types custom fields in the $fields array, but the wpcf- prefix should not be used in the $errors array. Maybe there is another difference in the fields we are validating.
- What type of field is application-tour-number?
- Is it a Types custom field, or a generic field?
- Does this field allow multiple values (repeating field), or only one value?

#1992223
Screenshot 2021-03-18 at 10.08.59 PM.png

Dear Christian Cox,

Answer your question as below

application-tour-number is a single line field

I put below code in CRED form

[cred_field field='application-tour-number' force_type='field' class='form-control ' output='bootstrap' urlparam='tour-id']

The field is only one value

#1992287

Something else must be going on, because this syntax is correct for a single line text field:

function fn_cred_form_validate($error_fields, $form_data)
{
    list($fields,$errors)=$error_fields;
 
    if ($form_data['id']==221) {
        if (empty($fields['wpcf-application-tour-number']['value'])) {
            //set error message for my_field
            $errors['application-tour-number']='Please select tour first';
        }
    }
    return array($fields,$errors);
}
add_filter('cred_form_validate','fn_cred_form_validate',10,2);

I just tested this in a local environment and a validation message is displayed when the empty form is submitted. I need to log in and take a closer look. Can you provide:
- Login credentials
- URL where I can see the Form on the front-end of the site
- Where can I find the custom code with cred_form_validate hook?

#1995265
empty-tour.png
application-post.png

I just tested the Form here:
hidden link

I see an error message if the Tour Number field is empty. See the screenshot empty-tour.png here. The error message says "The post was not saved because of the following problem: Tour Number: Please select tour first".

If I add a tour number like 123, the Form submits successfully and I am redirected to the single Application post:
hidden link

See application-post.png here. Everything seems to work as expected in this simple test. What am I missing?

#1995465

My issue is resolved now. Thank you!