-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-validation.php
More file actions
51 lines (36 loc) · 1.27 KB
/
class-validation.php
File metadata and controls
51 lines (36 loc) · 1.27 KB
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
<?php
/**
* Add custom validation to the back-end of the forms.
* - Check user's DOB and ensure the user is 21 or older. Fishbowl's API will auto-reject any leads who are under (14?)
*
* NOTE: Front-end validation happens separately via javascript. This is an extra layer on the back-end to check before sending to Fishbowl.
*/
namespace GF_Fishbowl;
class Validation {
public function __construct() {
add_filter( 'gform_field_validation', [$this, 'is_user_old_enough'], 10, 5 );
}
public function is_user_old_enough( $result, $value, $form, $field, $entry ) {
if ( 'Birthday' !== $field->label ) {
return $result;
}
$min_age = 21;
if ( $result['is_valid'] ) {
if ( is_array( $value ) ) {
$value = array_values( $value );
// error_log( print_r( $value, true ) );
}
$date_value = \GFFormsModel::prepare_date( $field->dateFormat, $value );
$today = new \DateTime();
$diff = $today->diff( new \DateTime( $date_value ) );
$age = $diff->y;
// error_log( 'User is ' . $age . ' years old.' );
if ( $age < $min_age ) {
// error_log( 'User is under ' . $min_age . ' years old.' );
$result['is_valid'] = false;
$result['message'] = 'Sorry, you must be at least ' . $min_age . ' years old.';
}
}
return $result;
}
}