Skip Navigation

[Resolved] I want a form to submit only if someone correctly enters field value from post

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

Problem:

How to stop the form from submitting unless a correct value is added and compared with a custom field

Solution:

- Add a generic field in the form.

- Use the cred_form_validate hook to check against the entered field and the custom field in question:

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

Add the custom code below according to the documentation:

<?php
add_filter('cred_form_validate','chr_validation',10,2);
function chr_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
    //print_r($fields);
    //validate if specific form
    if ($form_data['id'] == FORMID)
    {
       
        //check my_field value
        if ($fields['generic-field']['value'] != types_render_field("custom-field", array()) )
        {
            //set error message for my_field
            $errors['generic-field'] = 'Please enter a correct PIN.';
        }
    }
    //return result
    return array($fields,$errors);
}


Replace FORMID with the ID of your form.


Replace custom-field with the slug of the custom field that you want to check against.


Replace generic-field with the slug of the generic field option you added in the form.

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

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.

This topic contains 13 replies, has 2 voices.

Last updated by Elsie 1 year, 11 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2353823

Tell us what you are trying to do? I have a form that will update a post. But I only want the form to submit if someone correctly enters one of the post field values. (For instance, I'm handing out cards with a PIN number. To update the post using my form, they must enter the correct PIN number. That way they are the only one who can update the post.)

Is there any documentation that you are following? I searched your support archives and could not find a solution

Is there a similar example that we can see? No

What is the link to your site? hidden link

#2354395

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Please do as follows:

In the Edit form that you have, add a Generic Field with the type of Number or Text and add the name of "the-pin" for that field.

Use the method below to add a custom code to your website:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

The custom code should be something like this:


add_action('cred_before_save_data', 'chr_before_save_data_action',10,1);
function chr_before_save_data_action($form_data)
{
    // if a specific form
    if ($form_data['id']==999)
    {
        if (isset($_POST['the-pin']))
        {
            if ($_POST['the-pin'] != types_render_field("slug-of-the-pin-custom-field", array())) {
				DIE("Please enter a correct PIN");
			}
        }
    }
}

In the code above:

Change 999 with the Post Form ID that you have to edit the post.

Change slug-of-the-pin-custom-field with the slug of the pin custom field that you have on your post.

Description of the code

The code above uses the Toolset forms "cred_before_save_data" hook to trigger the check just before saving the data. For more information:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

First, it checks that the form ID is what you have for the edit post, then it checks if the PIN field is filled by the visitor and compares it with the custom field that you have on your post type for the PIN. If the visitor adds something different than the custom field, it triggers the DIE php function which halts the saving of the data with a message informing about the wrong pin:

hidden link

Also to retrieve the content of the PIN custom field we used the method below:

https://toolset.com/documentation/customizing-sites-using-php/functions/#numeric

Please kindly consider that this is a customization request and we can not implement the code for you. The reply above is an attempt to give you pointers on how to get started and implement the functionality that you have in mind. If you need additional customization about this matter we do suggest that you consider hiring a developer.

Thank you.

#2357363

Thank you! So, I got this added, and it's stopping the form from submitting, but it's not showing any message. It's not showing your DIE message or a message from the form. So close!!! Any ideas?

#2358097

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Maybe you did not add the PIN IDs correctly?

I'd be happy to take a look at it.

I'd appreciate it if you could give me the URL/User/Pass of your WordPress dashboard after you make sure that you have a backup of your website.
It is absolutely important that you give us a guarantee that you have a backup so if something happens you will have a point of restoration.

Make sure you set the next reply as private.

Please give me the details of which custom field that I need to check and which form you are talking about and the front end URL.

#2359839

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I managed to log in into your website but I could not find the page where you added the form in to test. WOuld you please give me the link to the page so that I can check the front end?

Thank you.

#2359843

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I managed to log in into your website but I could not find the page where you added the form in to test. WOuld you please give me the link to the page so that I can check the front end?

Thank you.

#2360015

It's not really on a page. It pulls the content format from an entry.

hidden link

#2360615

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I spent a lot of time on this and could not find why this is not working for your installation. For me, it is working like a charm.

I'd appreciate it if you could get back to me with the user/pass of a staging or development version of your website so that I can turn off plugins and do additional testing.

You can ask your hosting service provider to give you a staging version of your website.

Thank you.

#2361615

Hi there,

My site is backed up daily and it is not live yet, so you can disable plugins and do testing on it.

Elsie

#2363647

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Please kindly check the custom PHP file now and it is working ok.

I deactivated all the plugins except the Toolset ones and it is working ok.

Thank you.

#2363923

I do not know which custom PHP file you're talking about, but there is still an issue. While this theoretically works, the "die" function breaks the page if the wrong PIN is entered. Is there not a more eloquent way to do this? The message "Please enter a correct PIN" comes up, but the form is gone and the page is blank.

#2363931

Hey there. I found a way to format it so it doesn't look so bad. This seems to interfere with the The Events Calendar plugin, in case you want to note that. I'm not sure how I'm going to fix that yet. Might have to use a different events plugin.

#2364155

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I took another approach and used the Validation check in the Forms API Hook detailed below:

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

The final result in the custom code added inside Toolset > Settings > Custom Code is:

add_filter('cred_form_validate','chr_validation',10,2);
function chr_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
    //print_r($fields);
    //validate if specific form
    if ($form_data['id'] == 3120)
    {
      
        //check my_field value
        if ($fields['4-digit-pin-verification']['value'] != types_render_field("3-digit-pin", array()) )
        {
            //set error message for my_field
            $errors['4-digit-pin-verification']='Please enter a correct PIN.';
        }
    }
    //return result
    return array($fields,$errors);
}

If you check now it has a better and more standard way of handling the validation and it does not use the EXIT functionality.

For The Events Calendar problem please use the workaround mentioned in the errata below:

https://toolset.com/errata/events-calendar-update-breaks-toolset-post-forms/

Thank you.

#2368627

That is a great solution! Thank you!

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