WordPress Gravity Form Field Validation - php

I have a WordPress sites using Gravity Forms, and within this form I have a field that requires a special 10 digit code to be input by the user that matches up with a database of codes (there are 20 of these codes). If the code that has been input matches one of the 20 codes, then they can proceed with submitting the form. If it doesn’t match then they cannot proceed.
Please advise on how to create a PHP function that achieves this?
Many thanks

I wrote a snippet that you might find helpful.
http://gravitywiz.com/require-existing-value-submission-gravity-forms/
To use this snippet for your needs, you would need to setup a new form that will be used to store the 20 codes. Add a field a Single Line Text field to your form. Add an entry for each code.
On your original form, you can now configure the snippet to check the values of the new form for a valid code. Happy to answer any questions.
new GW_Value_Exists_Validation( array(
'target_form_id' => 613, // your original form ID where the codes will be validated
'target_field_id' => 1, // the field on your original form where the user should enter the code
'source_form_id' => 519, // the new form you will create to store your codes
'source_field_id' => 1, // the field on the new form that will store one code per submission
'validation_message' => 'Hey! This isn\'t a valid reference number.' // the error message displayed if the user does not enter a valid code
) );

Related

Contact Form 7 & Flamingo - Getting Flamingo Post ID as a Mail tag

On a WordPress website I'm using Contact Form 7 and Flamingo to manage contact forms, and store the data.
I've been using [_serial_number] in my emails to identify a submission which is stored in Flamingo. This serial number is added to a link which opens a page, and queries the database for the submission (by serial number) to display all the information online for the user who gets the email.
I've realised today that the serial number is reset for each form you create.
Eg. Form 1 submission serial numbers start at 1, and increments to 10 (for example).
If I then make Form 2, submission serial numbers start at 1 again.
This is causing a problem because there are multiple posts with the same serial number, so I'm not guaranteed to get the right submission.
I can't see a way of getting the Flamingo Post ID as a mail tag anywhere, I've looked through the code for Flamingo and can't see any hooks that would let me add in the ID of the post as a Mail Tag in CF7.
Does anyone know if this is possible?
I solved this issue by creating and storing my own unique token with each form submission and getting Flamingo posts by that via a meta_query instead of trying to get posts by the serial_number. Unfortunately it won't solve your problem for historical submissions that lack the custom token.
Create a field for the token in the CF7 Form tab. In my case I'm using CF7 Dynamic Text Extension to get a cookie value but it could even be static text. You just need a unique field that you can modify later.
[dynamichidden prtoken "DT_CF7_COOKIE key='dealer_id'"]
Generate the actual token value and replace the field content with it. I used the field value + unix time + a pseudo random blob so that I can see who generated it and when but it isn't guessable. My application is relatively low risk and I'm not a security expert.
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
if( !empty( $array['prtoken'] ) ) {
//Generate real token
$prtoken = $array['prtoken'] . '_' . time() . '_' . bin2hex(random_bytes(8));
if( !empty( $prtoken ) ) {
$array['prtoken'] = $prtoken;
}
}
return $array;
};
In my case the token forms part of a query string on a link in the email template:
Reply
Get posts matching the token. Should only ever be 0 or 1 but treat it as if it might match multiple flamingo items, just in case.
$args = array(
'post_type' => 'flamingo_inbound',
'fields' => 'ids', //Only return IDS
'meta_query' => array(
array(
'key' => '_field_prtoken', //The custom token field
'value' => sanitize_text_field($field_prtoken), //The token is input by users retrieving data so needs to be sanitized.
)
)
);
$postslist = get_posts( $args );

How can I get data from Ninja Forms to appear in frontend?

I just know a little bit about Javascript, I'm not a programmer yet.
I created a Ninja Forms to be used in my Wordpress site. When a user fills out the 4 fields of the form and press Submit, I need those data to appear always in the frontend ( in their 4 specific places I've created in html), and the last one submitted overwrite to the previous one.
Ninja is saving the data in wp_postmeta like this:
**meta_key** **meta_value**
_field_12 (the first of the values I want appear in frontend)
_field_13 (the second one)
_field_14 (the third one)
_field_15 (the fourth one)
_form_id 1 (always the same value)
_seq_num 3 (increase 1 every time Submit is pressed)
How can I do it?
I know is out of my range without PHP knowledge but, can someone give me some trick? Is there a plugin to get it?
Help, please
You have several options:
Create a custom Ninja Forms Action. This obviously will require significant amount of programming skills, understanding how Ninja Forms works, etc. Advantage, you could store your data in preferred location and format (e.g. in wp_options table).
Hackfix the problem. You can probably do something like this in your template:
.
$form_id = 3;
$submissions = Ninja_Forms()->form( $form_id )->get_subs();
if ( is_array( $submissions ) && count( $submissions ) > 0 ) {
// Get first element of array; latest submission
$latest_submission = reset( $submissions );
// Returns array with all submission values
$all_fields = $latest_submission->get_field_values();
print_r( $all_fields );
// To get/display single value
$single_field = $latest_submission->get_field_value( 'firstname_1531139833971' );
echo $single_field;
}
If you not sure about form ID, check the id listed in shortcode (Ninja Forms -> Dashboard, then shortcode column). E.g. [ninja_form id=3] id=3 is your form_id.
Easiest way to obtain key for method get_field_value is to print_r all submission values and check which one is which one, otherwise, click to edit form, select field to edit, expand administration section and you should see the key for that field.
Option #1 should be preferred method to address this issues, if you intend to proceed with option #2 make sure to check function Ninja_Forms() exists first, otherwise you will get fatal errors if Ninja Forms is disabled/not loaded/etc.

How to show a field after validateForm() is called in Drupal 8?

Inside a hook_form_FORM_ID_alter() function I show or hide a field based on another checkbox field is checked or not with the following code in Drupal 8.
$form['field_my_url']['#states'] = [
'visible' => [
':input[name="field_my_checkbox[value]"]' => ['checked' => TRUE],
],
];
In case user checks field_my_checkbox checkbox, enters an invalid URL in field_my_url and decides to uncheck field_my_checkbox checkbox, the field_my_url will then be hidden with the invalid URL remaining. It will fail the validation because the enter URL is not valid.
The user will then be redirected back with the error message. But because the field_my_checkbox checkbox was not checked, the field_my_url field will be hidden with the error message and the user cannot see that.
In this case, how can I show the field_my_url field if it failed the validation because it contains invalid URL in Drupal 8?
I have found a solution. It might not be efficient. But it solves the problem for now, since there is no one come up with any solution on this moment. If the validation failed. I just unset the '#states' from the field. By doing this, no #states visible will be applied and the field_my_url will be show as normal.
unsert($form['field_my_url']['#states']);

How to enumerate the forms in cf7?

I'm a newbie to web designing. I'm using contact form 7 to create a registration form for our conference.
All I wanted to do is, I need to give a unique id for all of them after they have registered for the conference and the further forms should be identified using this unique id.
So far, I have installed contact form 7 and contact form dtx
for this purpose and I have tried Koen de Bakker solution of generating a random number.
But it's slightly different from what I want, since it changes the random number for each refresh.
What I'd like is:
An unique number like "17ICLAA001,..." should be generated for each form submission.
Send the unique number to the applicant after successive submission of the form.(I hope this can be easily done once the shortcode is done).
Editing the form using the unique id.
Any help will be much appreciated. Thank you.
I have found a way to do this. Its just the number of rows+1 in the table.
When you add a record to your table the unique number also gets increased by 1 in the following code. Add the following function in function.php in your theme and use the short code "row_count" to call the function. Use it with dynamichidden text from dtx.
function row_count_shortcode() {
global $wpdb;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->username_wp1.SaveContactForm7_1" )+1;
return "17ICLAA".sprintf('%03d',$user_count);
}
add_shortcode( 'row_count', 'row_count_shortcode' );
Usually when you are creating contact forms using contact form7 it automatically creates a table in your database something like
username_wp1.SaveContactForm7_1
Instead of this replace your database table name.
So in your contact form, type
[dynamichidden uniqueid "row_count"]
and use [uniqueid] in your email body to serve your purpose.
It works fine. I have checked in my site.
Correct way to generate a unique and progressive number is to set a field in wp_option like this:
add_option('unique_number', '1');
When filter is called, you must simply increment this unique number:
function genTicketString() {
$currentUniqueNumber = get_option('unique_number');
$newCurrentUniqueNumber = $currentUniqueNumber + 1;
update_option('unique_number' $newCurrentUniqueNumber );
return $newCurrentUniqueNumber;
}
add_shortcode('quoteticket', 'genTicketString');

Initializing the php form created with Formtools.org

I'm working on a form with Form tools http://www.formtools.org
My task is to create php multi-page form, using their API. And it should also save data after every step, not only after the last one.
But I already have a problem while initializing the form into the system.
I have all pages built, and when I'm doing test submission it works good. I successfully see my 'Thank you page'.
Following is the code from my 1st page
<?php
require_once("/app-administration/global/api/api.php");
$fields = ft_api_init_form_page("", "test");
$params = array(
"submit_button" => "submit",
"next_page" => "lc_2.php",
"form_data" => $_POST
);
ft_api_process_form($params);
?>
Then, due to the tutorial, I go to the admin panned, create new form there. And then, I need to initialize it.
So, I'm changing this line:
$fields = ft_api_init_form_page(12, "initialize");
But then, when I try to submit the form I get 100 error on the last page:
http://docs.formtools.org/api/index.php?page=error_codes#100
It says there is something with ID, but 12 is correct one because I have it from admin panel, and I double-checked it there.
Anybody have any ideas why I might have this error? Any help will be appreciated.
Thanks

Categories