Skip Navigation

[Resolved] Need to enforce a unique field even on edit

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

Last updated by Jamal 3 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#2023565

I'm using the fix at https://toolset.com/forums/topic/need-to-validate-unique-values-in-post-field/ to force unique data in a field. I want to also enforce on edit (so have to filter out the post being edited). how do I change it to do that?

#2023617

Hello and thank you for contacting the Toolset support.

Let's take the first custom code in the ticket that you referred to. https://toolset.com/forums/topic/need-to-validate-unique-values-in-post-field/#post-1201756

From line 8 to 31, we check the form ID and if it is our form's ID, we check if there are posts that have the same field value. This query will fail in the case of an edit form because the current post already exists on the database and has the same value. We need to exclude the current post from the search. Change the lines 10-17 to:

        $current_post = $fields['id']['value']
        $args = array(
                    'meta_query' => array(
                        array('key' => 'wpcf-EMAIL-FIELD-SLUG',
                              'value' => $_POST['wpcf-EMAIL-FIELD-SLUG']
                            )),
                    'post_type' => 'your-post-type',
                    'posts_per_page' => -1,
                    'post__not_in' => array($current_post),
                    );

https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

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

#2023653

Hmmm. I changed my IF to handle both form ids (edit and post) and changed the args to:

$args = array(
'meta_query' => array(
array('key' => 'wpcf-field',
'value' => $_POST['wpcf-field']
)),
'post_type' => 'post-type',
'posts_per_page' => -1,
'post__not_in' => array($current_post),
);

But it's still pulling up the post (counting as one returned from the query).

#2024121

Would you allow me temporary access to your website to check this closely? I will also need FTP access. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

Please provide the following information:
- The create form. And where I can see it in the frontend?
- The edit form. And where I can see it in the frontend?
- Where did you add the custom code?

#2024871

Thank you for the WordPress credentials. I was able to log in. However, I could not find the slab_validation.php file in the child theme's files. I also could not find it on some plugins I suspected.

I tried to use FTP to browse the site, maybe the file's location is not correct. But, the FTP credentials are not working for me.

Please let me know how to find this file?
Let me know where the form to create new Slabs is located, so I can test it too with the custom code.

#2025151

Thanks. Sorry about that. Forgot to add that for the ftp it uses port 26337 for sftp.

It's the tales-tavern plugin that has the function and it's in the plugins/tales-tavern/plugin_sections folder that you'll find the slab_validation file.

You should be able to get to the edit form at the url I listed before for toolset-test, It's the edit form for the slab post type (Seen in the toolset dashboard).

#2025189

Can you test again now? This is the final code:

add_filter('cred_form_validate','func_validate_slab',10,2);
function func_validate_slab($error_fields, $form_data){

    //field data are field values and errors

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

     if ($form_data['id']==94 || $form_data['id']==100){
       $args = array(
                   'meta_query' => array(
                       array('key' => 'wpcf-slab-code',
                             'value' => $_POST['wpcf-slab-code']
                           )),
                   'post_type' => 'slab',
                   'posts_per_page' => -1,
                   // 'post__not_in' => array($current_post),
                   );
		 if ( $form_data['id']==100 ) {
			 $current_post = $_POST['_cred_cred_prefix_post_id'];
			 $args['post__not_in'] = array($current_post);
		 }

            $posts = get_posts($args);


        //check if birthday value is already on the database

        if (count($posts) > 0){

            //set error message for my_field

            $errors['wpcf-slab-code']='This code is already saved on another board or slab ';

        }

    }

    return array($fields,$errors);
}

Notice how I commented line 16, then introduced it in lines 18-21.

Side note: I suggest that you convert the slab code field into a single line field instead of a Textarea field unless you are sure that codes will be too long or will contain multiple lines.