|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms\Blocks; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Riclep\StoryblokForms\MultiInput; |
|
7
|
|
|
use Riclep\StoryblokForms\Traits\HasNames; |
|
8
|
|
|
use Riclep\StoryblokForms\Traits\InFieldset; |
|
9
|
|
|
use Riclep\StoryblokForms\Traits\ToJson; |
|
10
|
|
|
|
|
11
|
|
|
class LsfConditionalSelect extends MultiInput |
|
12
|
|
|
{ |
|
13
|
|
|
use HasNames, InFieldset, ToJson; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected string $optionsName = 'options'; |
|
19
|
|
|
|
|
20
|
|
|
protected string $type = 'conditional-select'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Returns all the validation rules for the field |
|
24
|
|
|
* |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
public function validationRules(): array |
|
28
|
|
|
{ |
|
29
|
|
|
// TODO - loop over all children looking for required validation and update to required_if |
|
30
|
|
|
|
|
31
|
|
|
$rules = []; |
|
32
|
|
|
|
|
33
|
|
|
$this->fields->filter(function($field) { |
|
|
|
|
|
|
34
|
|
|
return $field->component() !== 'lsf-text-note'; |
|
35
|
|
|
})->each(function ($field) use (&$rules) { |
|
36
|
|
|
$rules = array_merge($rules, $field->validationRules()); |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
$fieldRules = parent::validationRules(); |
|
40
|
|
|
|
|
41
|
|
|
// Should the Dot name always do this? Probably not as that would break children? |
|
42
|
|
|
$selectKey = $this->getInputDotNameAttribute() . '.selected'; //- is that needed in some places? |
|
43
|
|
|
|
|
44
|
|
|
if (array_key_exists($this->getInputDotNameAttribute(), $fieldRules)) { |
|
45
|
|
|
return array_merge($rules, [$selectKey => $fieldRules[$this->getInputDotNameAttribute()]]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $rules; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns all the error messages for the field |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function errorMessages(): array |
|
56
|
|
|
{ |
|
57
|
|
|
$rules = []; |
|
58
|
|
|
$selectMessage = []; |
|
59
|
|
|
|
|
60
|
|
|
$this->fields->each(function ($field) use (&$rules) { |
|
|
|
|
|
|
61
|
|
|
$rules = array_merge($rules, $field->errorMessages()); |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
$messages = $this->validators->errorMessages(); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
if ($this->parent() instanceof LsfConditionalSelect) { |
|
67
|
|
|
foreach ($messages as $key => $rule) { |
|
68
|
|
|
if (Str::endsWith($key, 'required')) { |
|
69
|
|
|
$messages[$key . '_if'] = $rule; |
|
70
|
|
|
|
|
71
|
|
|
unset($messages[$key]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$selectKey = $this->getInputDotNameAttribute() . '.selected.required_if'; |
|
76
|
|
|
$selectMessage = [$selectKey => $messages[$this->getInputDotNameAttribute() . '.required_if']]; |
|
77
|
|
|
} else { |
|
78
|
|
|
if (array_key_exists($this->getInputDotNameAttribute() . '.selected.required', $this->validationRules())) { |
|
79
|
|
|
$selectKey = $this->getInputDotNameAttribute() . '.selected.required'; |
|
80
|
|
|
$selectMessage = [ |
|
81
|
|
|
$selectKey => $messages[ |
|
82
|
|
|
$this->getInputDotNameAttribute() . '.required' |
|
83
|
|
|
] |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (array_key_exists($this->getInputDotNameAttribute() . '.selected', $this->validationRules())) { |
|
88
|
|
|
$selectKey = $this->getInputDotNameAttribute() . '.selected'; |
|
89
|
|
|
$selectMessage = [ |
|
90
|
|
|
$selectKey => $messages[ |
|
91
|
|
|
$this->getInputDotNameAttribute() . '.required' |
|
92
|
|
|
] |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return array_merge($rules, $selectMessage); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the Input’s response after the form has been submitted and validated |
|
103
|
|
|
* All options are returned as an array with their name and a selected boolean |
|
104
|
|
|
* based on the user’s input |
|
105
|
|
|
* |
|
106
|
|
|
* @param $input |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function response($input): array |
|
110
|
|
|
{ |
|
111
|
|
|
$formatted = [ |
|
112
|
|
|
'label' => $this->label, |
|
|
|
|
|
|
113
|
|
|
'name' => $this->name, |
|
|
|
|
|
|
114
|
|
|
'response' => ['select' => ['selected' => [], 'unselected' => []]], |
|
115
|
|
|
'type' => $this->type, |
|
116
|
|
|
]; |
|
117
|
|
|
|
|
118
|
|
|
$this->options()->map(function ($formInput) use ($input, &$formatted) { |
|
119
|
|
|
if ($input && $formInput['value'] === $input['selected']) { |
|
120
|
|
|
return $formatted['response']['select']['selected'][$formInput['value']] = $formInput['label']; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $formatted['response']['select']['unselected'][$formInput['value']] = $formInput['label']; |
|
124
|
|
|
})->toArray(); |
|
125
|
|
|
|
|
126
|
|
|
$formatted['response']['fields'] = $this->fields->map(function ($field) use ($input) { |
|
|
|
|
|
|
127
|
|
|
if (!$input) { |
|
128
|
|
|
return $field->response($input); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
///// no required child fields? |
|
132
|
|
|
// Handle empty radio buttons etc. sending nothing in POST request |
|
133
|
|
|
if (!array_key_exists($field->name, $input)) { |
|
134
|
|
|
$input[$field->name] = null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $field->response($input[$field->name]); |
|
138
|
|
|
})->keyBy('name')->toArray(); |
|
139
|
|
|
|
|
140
|
|
|
return $formatted; |
|
141
|
|
|
} |
|
142
|
|
|
} |