Skip Navigation

[Resolved] Form Validation

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 8 replies, has 2 voices.

Last updated by fahimS-2 1 year, 10 months ago.

Assisted by: Minesh.

Author
Posts
#2571935

I have a post edit form which collects user data for a custom field. Now I want a form validation so that the user input value is not empty and less than 5. How can I do that?

#2572051

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can use the Toolset form hook "cred_form_validate" in order to validate you form for any form field you added to your form:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

You can add the following code to "Custom code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data) {
    //field data are field values and errors
    list($fields,$errors)=$field_data;
     
    //validate if specific form
    if ($form_data['id']==99999) {
      
      	if (trim($fields['wpcf-book']['value']) == '' or $fields['wpcf-book']['value'] > 6){
          $errors['wpcf-book']='Please enter less than or equal to 5.';
        }
         
    }
      
    //return result
    return array($fields,$errors);
}

Where:
- Replace 99999 with your original form ID
- Replace the book with your original field slug.

You are welcome to adjust the code as required if needed.

#2572123

Thanks for the code. It is working perfectly. How can I show the errors in the frontend?

#2572131

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What you mean it should display error when applicable on frontend when you submit the form.

#2572513

Yes, when the error occurs after submitting the form, I want to display it on the frontend. Is it possible?

#2572551

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What you mean by frontend. Toolset Form works on frontend and it should display the error on frontend.

Can you please share exact steps or video where exactly you want to display the error?

#2574213

Hi, thanks for your reply. When an user provides a value grater than 5 or a null value and submits the form, I want to show the error message below the form. How can I do that ?

#2574235

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You can edit your form in expert mode and add the following shortcode just after the submit button:

[cred_field field='form_messages' class='alert alert-warning']
#2574613

My issue is resolved now. Thank you!