Skip Navigation

[Resolved] Setting field to “required” does nothing in CRED

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

Problem:

I have set a custom field "mileage" as required.

However, in the CRED submission form, this does nothing.

Is there a workaround such as setting a custom validation in the functions.php file that we can use?

Solution:

Yes, you can follow our document, use CRED filter hook to add your custom validations:

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

This hook provides custom validation for form fields

Relevant Documentation:

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

This support ticket is created 6 years, 8 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by PaulS4783 6 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#695362

hidden link

In the Classified's site I have set a custom field "mileage" as required.
However, in the CRED submission form, this does nothing.
It still accepts a blank field and then the listing throws an error warning message.

Why doesn't this work?

Is there a workaround such as setting a custom validation in the functions.php file that we can use?

#695642

Hello,

Q1) Why doesn't this work?

The problem you mentioned above is abnormal, in case it is a compatibility problem, please try this:

1) deactivate other plugins and switch to wordpress default theme, and test again

2) If the problem still persists, please provide a database dump file (ZIP file) of your website in below private detail box, also point out the problem page URL and CRED form URL, I need to test and debug it in my localhost, thanks

Q2) Is there a workaround such as setting a custom validation in the functions.php file that we can use?

Yes, you can follow our document, use CRED filter hook to add your custom validations:

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

This hook provides custom validation for form fields

#695727

In this example:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //validate if specific form
    if ($form_data['id']==12)
    {
        //check if featured image exists
        if (empty($fields['_featured_image']['value']))
        {
            //set error message for featured image
            $errors['_featured_image'] = 'Missing featured image';
        }
    }
    //return result
    return array($fields,$errors);
}

I want to check that each field is NOT empty.
Where can I find the machine readable field ID ? e.g. '_featured_image'

#695739

If the field is setup by CRED shortcode [cred_field], for example featured image:

[cred_field field='_featured_image' value='' urlparam='' output='bootstrap']

As you can see, it is attribute "field", which is the "field ID" you are looking for.

You can edit the CRED form, and find the field slug(ID) in the shortcode [cred_field].

More help:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field
Arguments
field. The field slug.

#695919

ok. i guess you can close this ticket.