PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.2
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.2
6.2.3 6.2.2 6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / app / Supports / Form / FormFieldRulesBuilder.php
kirki / app / Supports / Form Last commit date
FormFieldRulesBuilder.php 4 days ago
FormFieldRulesBuilder.php
109 lines
1 <?php
2
3 namespace Kirki\App\Supports\Form;
4
5 defined('ABSPATH') || exit;
6
7 use Kirki\App\Constants\Form\FormFieldTypes;
8
9 /**
10 * Turns a Kirki form's `fields` configuration into rules the framework
11 * Validator understands.
12 *
13 * A field's `type` maps to a format rule (email, number, ...) and `required`
14 * toggles between the `required`/`nullable` rule — the only two constraints
15 * the form builder actually lets users configure.
16 */
17 class FormFieldRulesBuilder
18 {
19 /**
20 * The format rule for each field type that maps directly onto a single
21 * Validator rule.
22 *
23 * @var array<string, string>
24 */
25 protected const FORMAT_RULES = [
26 FormFieldTypes::EMAIL => 'email',
27 FormFieldTypes::NUMBER => 'number',
28 FormFieldTypes::DATE => 'date',
29 // Preserves the legacy (imprecise) datetime-local pattern rather than
30 // the stricter `datetime` rule, to avoid rejecting values the old
31 // validator accepted.
32 FormFieldTypes::DATETIME_LOCAL => 'regex:/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/',
33 FormFieldTypes::TEL => 'regex:/^[0-9]{10}+$/',
34 ];
35
36 /**
37 * Build the Validator rules for a form's fields.
38 *
39 * @param array $fields The form's field configuration.
40 * @return array<string, array<int, string>>
41 */
42 public static function rules(array $fields)
43 {
44 $rules = [];
45
46 foreach ($fields as $name => $field) {
47 if (($field['type'] ?? null) === FormFieldTypes::FILE) {
48 continue;
49 }
50
51 $rules[$name] = static::rules_for_field($field);
52 }
53
54 return $rules;
55 }
56
57 /**
58 * Build the data to validate: submitted form data filtered strictly to
59 * configured fields (excluding file fields) with empty values normalized to null.
60 *
61 * The Validator only skips format rules for a field it never received;
62 * an empty string is still "received". Normalizing empty values to null
63 * here keeps optional fields from failing format checks while `required`
64 * still catches them via its own missing-value check.
65 *
66 * @param array $form_data The submitted form data.
67 * @param array $fields The form's field configuration.
68 * @return array
69 */
70 public static function data_for_validation(array $form_data, array $fields)
71 {
72 foreach ($fields as $name => $field) {
73 if (isset($form_data[$name]) && empty($form_data[$name])) {
74 $form_data[$name] = null;
75 }
76 }
77
78 return $form_data;
79 }
80
81 /**
82 * The Validator rules for a single field.
83 *
84 * @param array $field The field configuration.
85 * @return array<int, string>
86 */
87 protected static function rules_for_field(array $field)
88 {
89 $rules = [static::required_rule($field)];
90
91 if (isset(static::FORMAT_RULES[$field['type'] ?? null])) {
92 $rules[] = static::FORMAT_RULES[$field['type']];
93 }
94
95 return $rules;
96 }
97
98 /**
99 * The `required`/`nullable` rule for a field.
100 *
101 * @param array $field The field configuration.
102 * @return string
103 */
104 protected static function required_rule(array $field)
105 {
106 return !empty($field['required']) ? 'required' : 'nullable';
107 }
108 }
109