-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck all child entries don't exceed total field on parent form
67 lines (66 loc) · 3 KB
/
check all child entries don't exceed total field on parent form
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
add_filter('gform_validation', callback: function ($validation_result) {
//get form
$form = $validation_result['form'];
//check is nested/child form.
$maybe_nested_form = new GP_Nested_Forms($form);
$is_nested_form = $maybe_nested_form -> is_nested_form_submission();
//if not child form do nothing
if (!$is_nested_form){
return $validation_result;
}
//check if field called amount
foreach ($form['fields'] as $field){
//if there is then validate
if ($field -> label == 'Amount'){//change 'Amount' to the label on child forms. make sure its the same on each child form
//get parent form then all child form entries
$parent_id = $maybe_nested_form->get_parent_form_id();
$parent_form = GFAPI::get_form($parent_id);
$parent_form = new GP_Nested_Forms($parent_form);
$session = new GPNF_Session($parent_id);
$entry_id_array = $session->get_valid_entry_ids($session->get_cookie()['nested_entries']);
$entries_array = array();
foreach ($entry_id_array as $entryid){
if (is_array($entryid)){
foreach ($entryid as $id){
$entries_array[] = $id;
}
}
else{
$entries_array[] = $entryid;
}
}
$entries = $parent_form->get_entries($entries_array);
//add the total
$value = 0;
foreach ($entries as $entry){
$child_form_id = $entry['form_id'];
$child_form = GFAPI::get_form($child_form_id);
foreach ($child_form['fields'] as $child_field){
if ($child_field->label == 'Amount'){//change 'Amount' to the label on child forms. make sure its the same on each child form
$field_id = $child_field->id;
$value += intval($entry[$field_id]);
}
}
}
$value += $field->gppa_hydrated_value;
//check against total on parent form
$parent_form = GFAPI::get_form($parent_id);
foreach ($parent_form['fields'] as $parent_field){
if ($parent_field -> label == 'Total'){//change 'Total' to label of field on parent form
$calc = new GP_Advanced_Calculations();
$formula = $parent_field->calculationFormula;
$total = $calc->eval_formula($formula);
if ($value > $total){
$field -> failed_validation = true;
$field -> validation_message = "The amount you entered exceeds your total."; //change message to whatever you want message to be
$validation_result['is_valid'] = false;
}
$validation_result['form'] = $form;
return $validation_result;
}
}
}
}
$validation_result['form'] = $form;
return $validation_result;
});