Skip Navigation

[Résolu] Allow only one submission of the form

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to use cred_before_save_data to prevent a User from submitting the same form multiple times to create multiple posts, or to edit the same post more than once.

Solution: The cred_before_save_data hook should not be used to validate form submissions. Instead, use the cred_form_validate hook. This will allow you to return meaningful error messages to the front-end of the site. Here is an example showing how to prevent a User from creating more than one post, or how to prevent a User from editing a post where a specific custom field already has a value:

add_filter('cred_form_validate','profile_exists_validation',10,2);
function profile_exists_validation($error_fields, $form_data)
{
    global $current_user;
    //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']==12345)
    {
        //check custom field value
        if (isset($fields['wpcf-sel1']['value']) && $fields['wpcf-sel1']['value']!='')
        {
            //set error message for my_field
            $errors['wpcf-sel1']='This custom field has already been defined for the post.';
        }
 
 
        //check if current User is already the author of a "Profile" custom post
          $args = array(
          'author'          =>  $current_user->ID,
          'orderby'         =>  'post_date',
          'order'           =>  'ASC',
          'posts_per_page'  => 1,
          'post_type'       => 'profile'
        );
        $profiles = get_posts( $args );
        if ( sizeof($profiles) > 0 )
        {
            //set error message for post title
            $errors['post_title'] = 'Profile already exists, so you cannot create another one.';
        }
    }
    //return result
    return array($fields,$errors);
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
https://codex.wordpress.org/Template_Tags/get_posts

This support ticket is created Il y a 5 années et 9 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 4 réponses, has 2 voix.

Last updated by ioannisM-2 Il y a 5 années et 9 mois.

Assisted by: Christian Cox.

Auteur
Publications
#922416

Hi, I have the following issues:

1) If a user hits the back button he can submit a cred form multiple times. How could I use cred before save data to validate and allow the submition of the form only if a specific custom field is empty?

2) The users can create a profile that is a custom post type. How could use the same hook to validate and allow the creation of a profile post only once? I know I can use a conditional to hide the cred form if a user has a profile but he still can hit the back button and create multiple profiles

#922696

Hi, the cred_before_save_data hook will not prevent a Form from creating multiple posts or editing a post multiple times. You should set up this validation with the cred_form_validate hook instead. Here is an example you can review to see how this could work. In this example I am testing to see if a custom field value has already been set for the post being edited. I am also testing to see if the current User already has a profile post by querying posts with the current User as author. You probably won't use these two validations for the same form since one is for editing, the other is for creating, but you can get the idea.

add_filter('cred_form_validate','profile_exists_validation',10,2);
function profile_exists_validation($error_fields, $form_data)
{
    global $current_user;
    //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']==12345)
    {
        //check custom field value
        if (isset($fields['wpcf-sel1']['value']) && $fields['wpcf-sel1']['value']!='')
        {
            //set error message for my_field
            $errors['wpcf-sel1']='This custom field has already been defined for the post.';
        }


        //check if current User is already the author of a "Profile" custom post
          $args = array(
          'author'          =>  $current_user->ID,
          'orderby'         =>  'post_date',
          'order'           =>  'ASC',
          'posts_per_page'  => 1,
          'post_type'       => 'profile'
        );
        $profiles = get_posts( $args );
        if ( sizeof($profiles) > 0 )
        {
            //set error message for post title
            $errors['post_title'] = 'Profile already exists, so you cannot create another one.';
        }
    }
    //return result
    return array($fields,$errors);
}

Note that you must attach the "post already exists" error to some input field. I chose the post title. More information about this API: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Let me know if you have questions about this approach.

#924247

Christian thank you so much for your so clarifying answer! I have just a last question. Could it be possible, when showing the errors to the user to replace the label of the field with the value of the same field? Is that possible?

#924372

No, I'm afraid not. That part of the error message is not easily manipulated with code. We have an internal ticket in place to increase the flexibility of that area but it's not currently ready.

#924685

ok, thank you again Christian for your support!

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