Skip Navigation

[Resolved] Do not save cred form if user already created post

This support ticket is created 2 years, 10 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by Jamal 2 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#2099951

Hello, I need to disallow user to save data with toolset form, when he already sent one.

I have done some basic things like disallow displaying form if user post count is more than 1. But when user sent form and then go back in browser, form stays visible. So I would love to use the function else when user send form.

So is it possible to use this somehow with Toolset form?

<?php

function twiki_post_count() {
$user_post_count = count( get_posts( array(
'post_type' => 'kviz',
'author' => get_current_user_id(),
) ) );

if ($user_post_count >= 1) {
DO NOT SAVE FORM and redirect
} else {
SAVE FORM NORMALLY
}
}
?>

#2100003

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting Toolset support.

You will need to use the cred_form_validate hook and return an error when the user has already created a post. Check the examples in the hook's documentation https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Or check previous tickets about the use of this hook with this scoped searched link hidden link

I hope this helps. Let me know if you have any questions.

#2101035

Hello,

I created:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($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
 
    //validate if specific form
    if ($form_data['id']==178)
    {
 
            $user_post_count = count( get_posts( array(
                'post_type' => 'kviz',
                'author' => get_current_user_id(),
            ) ) );

            if ($user_post_count >= 1) {
                $errors['']='Kvíz už jsi vyplnil. Momentálně to lze pouze jednou.';
            }
    }
         
    return array($fields,$errors);
}
?>

And I don't know what to put inside the error? Because it is not a validation of any specific field.

#2101037

Hello, there is my code:


add_filter('cred_form_validate','my_validation',10,2);
function my_validation($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
 
    //validate if specific form
    if ($form_data['id']==178)
    {
            $user_post_count = count( get_posts( array(
                'post_type' => 'kviz',
                'author' => get_current_user_id(),
            ) ) );

            if ($user_post_count >= 1) {
                $errors['xxxxx']='Kvíz už jsi vyplnil. Momentálně to lze pouze jednou.';
            }
    }
         
    return array($fields,$errors);
}

I am not sure, what to put inside the $errors. I am not validating any fields, but if user have posts..

Thanks for advice.

#2101285

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Indeed, the error needs to be set against a field. Try using default fields, such as "post_title" or "post_content". I know, that's not as pretty as you would like to have, but it could serve to get the validation complete.

$errors['post_title']='Kvíz už jsi vyplnil. Momentálně to lze pouze jednou.';
// or
$errors['post_content']='Kvíz už jsi vyplnil. Momentálně to lze pouze jednou.';

If that's not enough, let me know and I'll escalate it to our 2nd Tier for further analysis. He may suggest another solution or check with the developers, but that could take some time before the request is answered.

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