PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.50
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.50
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Form / Settings / Validator / Notifications.php

Notifications.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.50, at app/Modules/Form/Settings/Validator/Notifications.php

91 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Form\Settings\Validator;
4
5 use FluentForm\Framework\Helpers\ArrayHelper;
6 use FluentValidator\Validator as FluentValidator;
7
8 class Notifications
9 {
10 /**
11 * Validates notifications settings data.
12 *
13 * @param array $data
14 *
15 * @return bool
16 */
17 public static function validate($data = [])
18 {
19 // Prepare the validation rules & messages.
20 list($rules, $messages) = static::validations();
21
22 // Make validator instance.
23 $validator = FluentValidator::make($data, $rules, $messages);
24
25 // Add conditional validations if there's any.
26 $validator = static::conditionalValidations($validator);
27
28 // Validate and process response.
29 if ($validator->validate()->fails()) {
30 wp_send_json_error(['errors' => $validator->errors()], 422);
31 }
32
33 return true;
34 }
35
36 /**
37 * Produce the necessary validation rules and corresponding messages
38 *
39 * @return array
40 */
41 public static function validations()
42 {
43 return [
44 [
45 'sendTo.type' => 'required',
46 'sendTo.email' => 'required_if:sendTo.type,email',
47 'sendTo.field' => 'required_if:sendTo.type,field',
48 'sendTo.routing' => 'required_if:sendTo.type,routing',
49 'subject' => 'required',
50 'message' => 'required',
51 ],
52 [
53 'sendTo.type.required' => 'The Send To field is required.',
54 'sendTo.email.required_if' => 'The Send to Email field is required.',
55 'sendTo.field.required_if' => 'The Send to Field field is required.',
56 'sendTo.routing' => 'Please fill all the routing rules above.',
57 ]
58 ];
59 }
60
61 /**
62 * Add conditional validations to the validator.
63 *
64 * @param FluentValidator $validator
65 *
66 * @return FluentValidator
67 */
68 public static function conditionalValidations(FluentValidator $validator)
69 {
70 $validator->sometimes('sendTo.routing', 'required', function ($input) {
71 if (ArrayHelper::get($input, 'sendTo.type') !== 'routing') {
72 return false;
73 }
74
75 $routingInputs = ArrayHelper::get($input, 'sendTo.routing');
76 $required = false;
77
78 foreach ($routingInputs as $routingInput) {
79 if (!$routingInput['input_value'] || !$routingInput['field']) {
80 $required = true;
81 break;
82 }
83 }
84
85 return $required;
86 });
87
88 return $validator;
89 }
90 }
91