Skip Navigation

[Resolved] Validate Post Title with cred_form_validate

This support ticket is created 2 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
- 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 1 reply, has 2 voices.

Last updated by Minesh 2 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2194591

Tell us what you are trying to do?
Make sure a post created with CRED is featuring unique title

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Is there a similar example that we can see?
Code hooked to cred_form_validate:

list( $fields, $errors ) = $error_fields;

if ( $form_data['id'] == $form_id ) {

    if ( isset( $fields['post_title'] ) && is_array( $fields['post_title'] ) ) {

        $new_item_name = esc_html( $fields['post_title']['value'] );
        $existing_items = post_exists( $new_item_name, false, false, 'post_type_name', 'publish' );

        if ( 0 !== $existing_items ) {
            $errors['post_title'] = 'Post Exists, choose another title';
        }
    }
}

$error_fields = array( $fields, $errors );

error_log( print_r( $error_fields, true ) );

return $error_fields;

Above shows in the debug log correctly an error in $error_fields for post_title saying:

['post_title'] = 'Post Exists, choose another title';

Yet, the form is still submitted, the post still created, and no error thrown!

The example on the DOC is not showing anything else than I do, thus, the mere act of passing an $errors['post_title'] = 'Post Exists, choose another title'; seems should suffice to trigger the Form to stop submit, throw error, and make user chose another name.

What am I doing wrong?

#2194773

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Hi Beda - I hope you are doing great.

I just checked with the following code with my test site and I've post exists with post title namely "Artist Four" and then if I try to create another post using the same post title "Artist Four" - it shows me the correct error. Please check the following screenshot:
=> hidden link

I used the following code:

add_filter( 'cred_form_validate', 'custom_validation1',10,2);
function custom_validation1( $error_fields, $form_data ) {
  
  
 list( $fields, $errors ) = $error_fields;
 
if ( $form_data['id'] ==  2622) {
 
  if ( isset( $fields['post_title'] ) && is_array( $fields['post_title'] ) ) {
 
        $new_item_name = esc_html( $fields['post_title']['value'] );
        $existing_items = post_exists( $new_item_name, false, false, 'artist', 'publish' );
      
       if ( 0 !== $existing_items ) {
            $errors['post_title'] = 'Post Exists, choose another title';
        }
 
      
    }
}
 
return array($fields,$errors);

}

Am I missing anything here?

Can you please confirm you used the correct priority for the validation hook.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.