Skip Navigation

[Resolved] add custom message to generic field

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.

This topic contains 1 reply, has 1 voice.

Last updated by Christopher Amirian 3 weeks, 1 day ago.

Assisted by: Christopher Amirian.

Author
Posts
#2792804
Screenshot 2025-01-18 at 10.13.54 PM.png

I have a custom generic field, I want to have my custom error message if no input to the field, please advise

      [cred_generic_field type='select' field='this-competition-id']
{
"required":1,
"default":[],
"options":[{"value":"8132","label":"第五屆全港中樂大賽"}]
}
      [/cred_generic_field]
#2792887

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. To display a custom error message when the cred_generic_field is left empty, you can achieve this using Toolset's built-in form validation features by leveraging the cred_form_validate hook as a custom code:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code

Add Validation Hook in custom code:

add_filter('cred_form_validate', 'custom_validate_competition_field', 10, 2);

function custom_validate_competition_field($error_fields, $form_data) {
    // Replace 1234 with your actual CRED form ID
    if ($form_data['id'] == 1234) {
        // Check if the custom select field is empty
        if (empty($_POST['this-competition-id'])) {
            $error_fields['this-competition-id'] = 'Please select a competition from the list.';
        }
    }
    return $error_fields;
}

- The cred_form_validate filter runs before form submission.
- Replace 1234 with your actual CRED form ID.
- The $_POST['this-competition-id'] checks the field value.
- If empty, the custom error message "Please select a competition from the list." is shown.

Display the Field in Your Form

Modify your form to include an error message placeholder for the field like this:

[cred_generic_field type='select' field='this-competition-id']
{
    "required":1,
    "default":[],
    "options":[{"value":"8132","label":"第五屆全港中樂大賽"}]
}
[/cred_generic_field]
<div class="cred-error-message" style="color: red;">[cred_field field='this-competition-id' error='true']</div>

Thanks.