Skip Navigation

[Resolved] cred_form_validate not returning error

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

Problem: I would like to use the cred_form_validate hook to apply an error on the post title under certain circumstances, but it does not seem to work.

Solution: Check the syntax and be sure the fields and values are referenced using the proper keys.

'name' => $fields['post_title']['value'],
...
$errors['post_title']='This Serial Number is already on file';

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

This support ticket is created 6 years, 5 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 2 replies, has 2 voices.

Last updated by coc-admin 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1077499

I am trying to: Add a custom validation for my "serial number" field. This is displayed to the user as Serial Number on the form, but is actually stored as the Post Title. I am validating that the post is not a duplicate serial/title. When stepping thru the code, it sets the error message correctly. But the message does not show, and the post continues to be added (creating a duplicate). My code is below:

add_filter('cred_form_validate','validate_dup_serial_nbr',10,2);
function validate_dup_serial_nbr($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//uncomment this if you want to print the field values
//print_r($fields);
//validate if specific form
if ($form_data['id'] == 1190 || $form_data['id'] == 1191) // Container Add or Edit forms
{
$serialExists = get_posts([
'post_type' => 'container',
'name' => $fields['post-title'],
'numberposts' => 1
]);

//check my_field value
if ($serialExists)
{
//set error message for my_field
$errors['post-title']='This Serial Number is already on file';
}
}
//return result
return array($fields,$errors);

Link to a page where the issue can be seen:

I expected to see: The message "This Serial Number is already on file"

Instead, I got: The post continued to add.This Serial Number is already on file

#1077534

I think you have the post title field label wrong, it should have an underscore not a hyphen. Like this:

$errors['post_title']='This Serial Number is already on file';

Let me know if that does not resolve the issue.

#1077580

That was it. Besides the underscore-vs-hyphen, I also was not qualifying enough to get the value. I had just ['post_title'] instead of ['post_title']['value'].

Thanks!