gw-require-list-columns.php
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gw-require-list-columns.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Require All Columns of List Field
* https://gravitywiz.com/require-all-columns-of-list-field/
*/
class GWRequireListColumns {
private $field_ids = array();
private $required_cols = array();
public static $fields_with_req_cols = array();
function __construct( $form_id = '', $field_ids = array(), $required_cols = array() ) {
$this->field_ids = ! is_array( $field_ids ) ? array( $field_ids ) : $field_ids;
$this->required_cols = ! is_array( $required_cols ) ? array( $required_cols ) : $required_cols;
if ( ! empty( $this->required_cols ) ) {
// convert values from 1-based index to 0-based index, allows users to enter "1" for column "0"
$this->required_cols = array_map( function( $value ) {
return $value - 1;
}, $this->required_cols );
if ( ! isset( self::$fields_with_req_cols[ $form_id ] ) ) {
self::$fields_with_req_cols[ $form_id ] = array();
}
// keep track of which forms/fields have special require columns so we can still apply GWRequireListColumns
// to all list fields and then override with require columns for specific fields as well
self::$fields_with_req_cols[ $form_id ] = array_merge( self::$fields_with_req_cols[ $form_id ], $this->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;
}
/**
* @var \GF_Field_List $field
*/
$values = rgpost( "input_{$field['id']}" );
// If we got specific fields, loop through those only
if ( count( $this->required_cols ) ) {
$rows = $field->create_list_array_recursive( $field->get_value_submission( array() ) );
foreach ( $rows as $row ) {
foreach ( $this->required_cols as $required_col ) {
$row = array_values( $row );
if ( rgblank( $row[ $required_col ] ) ) {
$new_validation_error = true;
$field['failed_validation'] = true;
$field['validation_message'] = $field['errorMessage'] ? $field['errorMessage'] : 'All required inputs must be filled out.';
}
}
}
} else {
// skip fields that have req cols specified by another GWRequireListColumns instance
$fields_with_req_cols = rgar( self::$fields_with_req_cols, $form['id'] );
if ( is_array( $fields_with_req_cols ) && in_array( $field['id'], $fields_with_req_cols ) ) {
continue;
}
foreach ( $values as $value ) {
if ( empty( $value ) && $value !== '0' ) {
$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 ( GFFormsModel::get_input_type( $field ) != '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 );
}
}
// Uncomment one of the following lines and customize to activate this snippet.
// 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));
// require specific field columns on a specific form
// new GWRequireListColumns( 240, 1, array( 2, 3 ) );