1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Riclep\StoryblokForms\Blocks\LsfConditionalSelect; |
7
|
|
|
use Riclep\StoryblokForms\Traits\HasNames; |
8
|
|
|
use Riclep\StoryblokForms\Traits\InFieldset; |
9
|
|
|
use Riclep\StoryblokForms\Traits\ToJson; |
10
|
|
|
|
11
|
|
|
class Input extends \Riclep\Storyblok\Block |
12
|
|
|
{ |
13
|
|
|
use HasNames, InFieldset, ToJson; |
|
|
|
|
14
|
|
|
|
15
|
|
|
protected $key; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Casting validators to Validators allow us to take control of how we process the |
19
|
|
|
* JSON from Storyblok by breaking out of the standard nested Blocks format |
20
|
|
|
* |
21
|
|
|
* @var string[] All the Validators for this Input |
22
|
|
|
*/ |
23
|
|
|
protected array $_casts = ['validators' => Validators::class]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Creates a key to be used for the VueJS :key on this input |
27
|
|
|
* |
28
|
|
|
* @param $key |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function loopKey($key): Input |
32
|
|
|
{ |
33
|
|
|
$this->key = $key; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* All the Validation rules for this Input |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function validationRules(): mixed |
45
|
|
|
{ |
46
|
|
|
if (!$this->validators) { |
|
|
|
|
47
|
|
|
return []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$rules = $this->validators->validationRules(); |
51
|
|
|
|
52
|
|
|
if ($this->parent() instanceof LsfConditionalSelect) { |
53
|
|
|
if (is_array($this->settings('lsf_is_conditional')['when_parent_is'])) { |
|
|
|
|
54
|
|
|
$requiredWhen = implode(',', $this->settings('lsf_is_conditional')['when_parent_is']); |
55
|
|
|
} else { |
56
|
|
|
$requiredWhen = $this->settings('lsf_is_conditional')['when_parent_is']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($rules as $key => $rule) { |
60
|
|
|
if (in_array('required', $rule, true)) { |
61
|
|
|
$requiredKey = array_search('required', $rule); |
62
|
|
|
|
63
|
|
|
$rules[$key][$requiredKey] = 'required_if:' . $this->parent()->input_dot_name . '.selected,' . $requiredWhen; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $rules; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* All the error messages for this Input |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
public function errorMessages(): mixed |
77
|
|
|
{ |
78
|
|
|
$messages = $this->validators->errorMessages(); |
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Rewrite required to required_if for items inside conditional selects |
82
|
|
|
*/ |
83
|
|
|
if ($this->parent() instanceof LsfConditionalSelect) { |
84
|
|
|
foreach ($messages as $key => $rule) { |
85
|
|
|
if (Str::endsWith($key, 'required')) { |
86
|
|
|
$messages[$key . '_if'] = $rule; |
87
|
|
|
|
88
|
|
|
unset($messages[$key]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $messages; |
94
|
|
|
} |
95
|
|
|
} |