Conditional Validation in ZF2 - forms

I have a form which starts with a Select with two options. There are several other fields, some of which are required for the first Select option, and the others required for the second Select option.
In the view, I'm using the Select to show/hide the relevant/irrelevant fields. Most of these fields are required when their option in the Select is selected.
What is the best way to only validate the fields that are relevant to whatever is selected in the Select?

In your controller, you could do something like that (the key is the form's setValidationGroup function):
//GET THE FORM
$form = $this->getServiceLocator()->get( '<FORM_NAME>' );
//GET THE POSTED DATA
$request = $this->getRequest();
$data = get_object_vars( $request->getPost() );
//DEPENDING ON THE SELECT VALUE, VALIDATE THIS OR THAT FIELDS
if ( $data[ 'SELECT_NAME' ] === 'A' ) {
$form->setValidationGroup( array( 'INPUT_A', 'INPUT_B' ) );
} else {
$form->setValidationGroup( array( 'INPUT_C', 'INPUT_D' ) );
}
//PERFORM THE FORM VALIDATION
if ( !$form->isValid() ) {
//FORM IS WRONG
( ... )
}
//FORM IS OK
( ... )

Related

Gravity Form Shortcode Confirmation

Hi I am echoing a shortcode of a gform but I need to check what the confirmation message is programmatically in php to call a function if successful and another if failed. Is it possible?
The confirmation message to be used is stashed in the GFFormDisplay::$submission property when the submission is processed so it can be retrieved when the form shortcode or block is processed on page render. You can access it like so:
$confirmation = rgars( GFFormDisplay::$submission, $form_id . '/confirmation_message' );
Alternatively you could use the gform_confirmation filter to override the confirmation to be used when the submission is being processed, before it is added to the GFFormDisplay::$submission property e.g.
add_filter( 'gform_confirmation', function ( $confirmation, $form, $entry ) {
if ( empty( $entry ) || rgar( $entry, 'status' ) === 'spam' ) {
// Return the default confirmation for spam.
return $confirmation;
}
// Check your condition here and replace the $confirmation if needed.
return $confirmation;
}, 11, 3 );

Gravityview - Show payment status of another form

I currently have Gravityview view which is getting the source from form A. However, I want to show if the current user has made a payment on form B. Obviously this can be fetched from the {payment_status} merge tag of form B. But How can I pull the data of form B onto the Gravity view custom content field of form A?
I've looked at the gform_entry_id_pre_save_lead hook but I think there's a better way. Thanks for your help in advance..
add_filter( 'gform_entry_id_pre_save_lead', 'my_update_entry_on_form_submission', 10, 2 );
function my_update_entry_on_form_submission( $entry_id, $form ) {
$update_entry_id = rgpost( 'my_update_entry_id' );
return $update_entry_id ? $update_entry_id : $entry_id;
}
You can either use the Gravityview Multiple Forms Add-on or you can try adding the following code to your functions.php:
function gf_check_form($atts = array()) {
$details = shortcode_atts( array(
'form_id' => "",
'user' => ""
), $atts );
$form_id = $details['form_id'];
$search_criteria = array(
'status' => 'active',
'field_filters' => array(
'mode' => 'all',
array(
'key' => 'created_by',
'operator'=> 'is',
'value' => $details['user']
)
)
);
$entries = GFAPI::get_entries( $form_id, $search_criteria);
foreach($entries as $entry){
$paid .= 'Paid on '.date("F d, Y", strtotime($entry['date_created'])).'<br><br>';
}
return $paid;
}
add_shortcode( 'check_form', 'gf_check_form' );
You can then add the following shortcode to your Gravityview in a custom content field:
[check_form form_id="Add the form ID of form B here" user="{created_by:ID}"]

Mform SELECT - Moodle

Is there mform select tag, when option is selected so that it highlights and stick to the selected one and select more options without the use of control key to select. I tried with selectMulitple, where it allows holding control key to select the options.
$select = $mform->addElement('select', 'course', get_string('course', 'core_course'), $options);
$mform->addHelpButton('course', 'course', 'core_course');
$mform->addRule('course', null, 'required', null, 'client');
$mform->setType('course', PARAM_INT);
$select->setMultiple(true);
Short answer is no. You need to use the control key to select multiple items in a select.
You could use a series of checkboxes though, something like this:
$courses = core_course_category::get(0)->get_courses(
array('recursive' => true, 'sort' => array('fullname' => 1)));
foreach ($courses as $course) {
$mform->addElement('advcheckbox', "courses[{$course->id}]",
format_string($course->fullname), null, array('group' => 1));
}
$this->add_checkbox_controller(1);
Then in your edit code, use something like this
for each($formdata->courses as $courseid => $selected) {
if ($selected) {
// User selected this course.
} else {
// User unselected this course.
}
}

How to use Gravity Forms gform_validation to ensure at least one of email or phone are entered

I'm a designer rather than a developer.
I'm using Gravity Forms. I have a simple Gravity Form:
[name]
[phone]
[email]
[message]
I'd like to ensure at least one of [phone] or [email] have been entered, rather than requiring both to be filled in.
Gravity Forms support say I can use gform_validation but I don't know how to build the code to validate the form such that if both [phone] and [email] are empty a message is displayed: please enter either phone or email.
Help appreciated.
In my opinion, it might be easier to do it this way:
Verify that one of phone or email inputs is filled by submitting the input data to a script. It could be JS or a PHP script. This can be done easily by using logical operators to check if both are empty.
Then use https://www.gravityhelp.com/documentation/article/gform_validation/#2-send-entry-data-to-third-party
For a singular form, using ID's to require atleast 1 of 2 fields being filled
This works great on smaller sites who only have 1 Gravity Form with an email field and phone field. Easily customizable. Most of the code is explained with comments.
<?php
// 1 = ID of form
add_filter( 'gform_validation_1', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result['form'];
// Our desired input fields
$phone = rgpost( 'input_6' );
$email = rgpost( 'input_7' );
// Fields that must be empty
if ( empty( $phone ) && empty( $email )) {
// Looping through the fields
foreach( $form['fields'] as &$field ) {
// Finds the field with ID of 7
// This is the field where the validation message will appear, can add multiple inbetween with ||-operator
if ( $field->id == '7' /*|| $field->id == '6'*/ ) {
$field->failed_validation = true;
$field->validation_message = 'Please enter either an email address or phone number.';
$validation_result['is_valid'] = false;
}
}
}
// Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
?>
Dynamic script that will run on all forms with input type="email" & input type="tel"
This works great on larger sites with multiple Gravity Forms. The script below will affect all Gravity Forms. Code is explained with comments.
<?php
add_filter( 'gform_validation', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result['form'];
// Finds current page
$current_page = rgpost( 'gform_source_page_number_' . $form['id'] ) ? rgpost( 'gform_source_page_number_' . $form['id'] ) : 1;
// Initiated when $current_page is true
if ( $current_page ) {
// Loops through all fields
foreach( $form['fields'] as &$field ) {
// Input types
$field_phone = $field["type"] == 'phone';
$field_email = $field["type"] == 'email';
// Accessing field value with rgpost()
$field_value = rgpost("input_{$field['id']}");
if ( $field_phone ) {
// Assigning the field value of field type phone
$field_phone_type = $field_value;
}
if ( $field_email ) {
// Assigning the field value of field type email
$field_email_type = $field_value;
// Only runs if theres both a field type email AND field type phone
if (isset( $field_email_type ) && isset( $field_phone_type )) {
// If both the email and phone fields are empty
if ( empty( $field_phone_type ) && empty( $field_email_type )) {
// Validation message is applied to $field_email field only - can be modified to be both
$validation_result['is_valid'] = false;
$field->failed_validation = true;
$field->validation_message = 'Please enter either an email address or phone number.';
}
}
}
}
}
// Assign modified $form back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
?>

Retrieve the user id who posts a single entry of Gravity Form

Suppose that I know the entry ID ($lead_id), how can I retrieve the user who creates that entry.
I try to
$lead = RGFormsModel::get_lead( $lead_id );
$form = GFFormsModel::get_form_meta( $lead['form_id'] );
$values= array();
foreach( $form['fields'] as $field ) {
$values[$field['id']] = array(
'id' => $field['id'],
'label' => $field['label'],
'value' => $lead[ $field['id'] ],
);
}
But the user data is not there. I check sql database, and the user data is in wp_rg_lead table, not in wp_rg_lead_detail table.
Can anybody please help?
The information in the *_rg_lead table is accessible in the array that's returned by the get_lead function. So
user_id = $lead['created_by']
Also note that it's better to use the Gravity Forms API for retrieving the entry, specifically the get_entry function:
GFAPI::get_entry( $entry_id )
which will return an Entry object.