Quantcast
Channel: Snippets Archives - Gravity Wiz
Viewing all articles
Browse latest Browse all 196

Require All Columns of List Field

$
0
0

When you mark a List field as required, only a single input from any column is required. What if you want every input in every column to be required? Here is a snippet that let’s you do it.

This snippet was modified on September 13th, 2012. It has been modified to a class format to allow you to apply this functionality to a specific or form and/or specific fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
<?php
 
/**
* Require All Columns of List Field
* http://gravitywiz.com/2012/07/23/require-all-columns-of-list-field/
*/
 
class GWRequireListColumns {
private $field_ids;
function __construct($form_id = '', $field_ids = array()) {
$this->field_ids = !is_array($field_ids) ? array($field_ids) : $field_ids;
$form_filter = $form_id ? "_{$form_id}" : $form_id;
add_filter("gform_validation{$form_filter}", array(&$this, 'require_list_columns'));
}
function require_list_columns($validation_result) {
$form = $validation_result['form'];
$new_validation_error = false;
foreach($form['fields'] as &$field) {
if(!$this->is_applicable_field($field, $form))
continue;
$values = rgpost("input_{$field['id']}");
foreach($values as $value) {
if(empty($value)) {
$new_validation_error = true;
$field['failed_validation'] = true;
$field['validation_message'] = $field['errorMessage'] ? $field['errorMessage'] : 'All inputs must be filled out.';
}
}
}
$validation_result['form'] = $form;
$validation_result['is_valid'] = $new_validation_error ? false : $validation_result['is_valid'];
return $validation_result;
}
function is_applicable_field($field, $form) {
if($field['pageNumber'] != GFFormDisplay::get_source_page($form['id']))
return false;
if($field['type'] != 'list' || RGFormsModel::is_field_hidden($form, $field, array()))
return false;
// if the field has already failed validation, we don't need to fail it again
if(!$field['isRequired'] || $field['failed_validation'])
return false;
if(empty($this->field_ids))
return true;
return in_array($field['id'], $this->field_ids);
}
}
 
// Accepted Parameters
// new GWRequireListColumns($form_id, $field_ids);
 
// apply to all list fields on all forms
new GWRequireListColumns();
 
// apply to all list fields on a specific form
# new GWRequireListColumns(4);
 
// apply to specific list field on a specific form
# new GWRequireListColumns(4, 2);
 
// apply to specific list fields (plural) on a specific form
# new GWRequireListColumns(4, array(2,3));
view raw gistfile1.php This Gist brought to you by GitHub.

How do I install this snippet?

Just copy and paste the code above in your theme’s ”functions.php” file.

Do I need to configure this snippet to work with my form(s)?

Only if you want to apply this functionality to specific forms and/or specific fields. A default instantiation of this class (with no parameters) will apply this functionality to all list fields on all forms.

new GWRequireListColumns();

To apply this to a specific form, you would simply pass the form ID as the first parameter:

new GWRequireListColumns(4);

To apply this functionality to a specific form and a specific List field on that form, you would specify the form ID as the first parameter and the field ID as the second:

new GWRequireListColumns(4, 2);

To apply this functionality to a specific form and multiple List fields on that form, you would specify the form ID as the first parameter and an array of fields IDs as the second parameter:

new GWRequireListColumns(4, array(2,3);

Summary

This can be conveniently used in combination with the Set Number of List Field Rows by Field Value snippet.


Viewing all articles
Browse latest Browse all 196

Latest Images

Trending Articles



Latest Images