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
|
|
|
* @var string[] All the Validators for this Input |
19
|
|
|
*/ |
20
|
|
|
protected $_casts = ['validators' => Validators::class]; |
21
|
|
|
|
22
|
|
|
public function loopKey($key) { |
23
|
|
|
$this->key = $key; |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* All the Validation rules for this Input |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function validationRules() { |
35
|
|
|
$rules = $this->validators->validationRules(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
if ($this->parent() instanceof LsfConditionalSelect) { |
38
|
|
|
if (is_array($this->settings('lsf_is_conditional')['when_parent_is'])) { |
39
|
|
|
$requiredWhen = implode(',', $this->settings('lsf_is_conditional')['when_parent_is']); |
40
|
|
|
} else { |
41
|
|
|
$requiredWhen = $this->settings('lsf_is_conditional')['when_parent_is']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($rules as $key => $rule) { |
45
|
|
|
if (in_array('required', $rule)) { |
46
|
|
|
$requiredKey = array_search('required', $rule); |
47
|
|
|
|
48
|
|
|
$rules[$key][$requiredKey] = 'required_if:' . $this->parent()->input_dot_name . '.selected,' . $requiredWhen; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $rules; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* All the error messages for this Input |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function errorMessages() { |
62
|
|
|
$messages = $this->validators->errorMessages(); |
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Rewrite required to required_if for items inside conditional selects |
66
|
|
|
*/ |
67
|
|
|
if ($this->parent() instanceof LsfConditionalSelect) { |
68
|
|
|
foreach ($messages as $key => $rule) { |
69
|
|
|
if (Str::endsWith($key, 'required')) { |
70
|
|
|
$messages[$key . '_if'] = $messages[$key]; |
71
|
|
|
|
72
|
|
|
unset($messages[$key]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $messages; |
78
|
|
|
} |
79
|
|
|
} |