Skip Navigation

[Resolved] Check if form value = custom field value and set radio to other value

This support ticket is created 4 years, 1 month 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Raja Mohammed 4 years, 1 month ago.

Assisted by: Raja Mohammed.

Author
Posts
#1839423

Hello awesome Toolset support,

we were selected to create website for Night of Science in Czech republic. We are preparing some kind of geocaching game with Toolset.

And we will really appreciate your help with submit button.

The situation is:

CPT = Tardigrades
CF = Unique code
CF = Found (radio button)

What we are trying to achieve is this: We would like to use Toolset Forms to change the radio button value on tardigrade with unique code which they find at the place where is that 3D printed Tardigrade found.

So the flow is:
1) The go to university landing page which we are building, look at the map for the not found Tardigrades in czech republic
2) Go to the place
3) find tardigrade with unique ID
4) Go to website form, write the ID
5) If the ID value is found and the Radio button is set to the 0 (Not found) at the post, set the radio button to 1 and redirect to thank you, please share page.
6) if the ID value is not found, refresh the page and show the notice at the top of page - Unique ID does not exist, try it again please.

Is it possible to do this kind of data verification with toolset forms?

Thank you very much. We really appreciate it.

Best Regards!

#1839589

Raja Mohammed
Supporter

Languages: English (English )

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

Hello there,

It seems like you want a form where the users input the tardigrades id and once they submit you check if the unique id already exists and if it is already found if not found you are changing the status as found ? I hope i got this right ?

You can make use of the Forms API in this case i would suggest using two filter cred_form_validate and cred_save_data

1) Using cred_form_validate you can validate the Unique ID by checking again the CPT Tardigrades, You might need some custom

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
 // Add the validation here,  The form fields can be obtained from $_POST

 // If unique id found return the form data
   // Add unique id check here

 // if not found return an error message
}

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

2) Once the validation in step 1 passed, Use cred_save_data to save the radio custom field value

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

I hope this helps better, let me know if you need more clarification

Kind regards
Raja

#1842655

Hello Raja,

I understand the way you think. Thats nice, and thanks for that. I only need some more informations.
1) I can not use the edit post form at other page than post.
- I need form, which is placed on page called "Verify your findings". When I set up the form to edit posts, then I cannot use it on this page.

2) I dont know how to build the validation.
I understand that I need to use $_POST (I am new in Programming, but I wil search what is it). To get what user filled in, so:

$userinput = $_POST["name"]

Then I need to check all posts in custom post type Tardigrades and their custom field called Unique ID (wpcf_unique_id). So this should be a database query or custom post query?

And if there is a Tardigrades post, which has same unique ID, then change value of radio button (wpcf_found) to 2. So I need something like this:

if ($userinput exists in some tardigrade post) {
get that post id and change it radio button to 2
}
else display error message and dont save.

Is this possible? Can you help?

#1842821

I built the snippet which works

/**
 * New custom code snippet (replace this with snippet description).
 */
toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

function my_validation() {
  if( isset($_POST['submit']) && isset($_POST['uniquecode']) ) {
      $input_value = $_POST['uniquecode'];
      $args = array(
        'post_type' => 'tardigrades',
        'numberposts' => -1,
        'meta_key' => 'wpcf-unique-code',
        'meta_value' => $input_value,
        'compare' => '='
      );
      $custom_posts = get_posts( $args );
        if( ! empty( $custom_posts ) ) {
        foreach ( $custom_posts as $p ) {
        update_post_meta($p->ID, 'wpcf-found', '2');
      }
  }
} 
}

Can this be implemented to forms? Or I can disable forms, and use my custom HTML and it will be easier?

I think only thing why I want to use the Forms is validation.

#1843177

Raja Mohammed
Supporter

Languages: English (English )

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

post_selection_on_edit_form.jpg
edit_form_link.jpg

I need form, which is placed on page called "Verify your findings". When I set up the form to edit posts, then I cannot use it on this page.

Please follow the instruction here on how to create a Edit form, https://toolset.com/course-lesson/front-end-forms-for-editing-content/
And while inserting the "Edit post link" You will have to option to choose a different page or post type which needs to be editor under the post selection check the attached screenshots

Yes, your validation can be implemented with forms as mentioned in the above response the custom function needs to be hooked to the filter add_filter('cred_form_validate','my_validation',10,2); Also in your code snippet it seems you are updating the data which is not recommended in the form_validation hook however if you are sure that it needs to be there then you can proceed with that.