1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms\Blocks; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Riclep\StoryblokForms\Input; |
7
|
|
|
|
8
|
|
|
class LsfAddress extends Input |
9
|
|
|
{ |
10
|
|
|
// Interface this.... |
11
|
|
|
|
12
|
|
|
protected $type = 'address'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* All the Validation rules for this Input |
16
|
|
|
* |
17
|
|
|
* @return mixed |
18
|
|
|
*/ |
19
|
|
|
public function validationRules() { |
20
|
|
|
$rules = $this->validators->validationRules(); |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Rewrite required to required_if for items inside conditional selects |
24
|
|
|
*/ |
25
|
|
|
if ($this->parent() instanceof LsfConditionalSelect) { |
26
|
|
|
if (is_array($this->settings('lsf_is_conditional')['when_parent_is'])) { |
27
|
|
|
$requiredWhen = implode(',', $this->settings('lsf_is_conditional')['when_parent_is']); |
28
|
|
|
} else { |
29
|
|
|
$requiredWhen = $this->settings('lsf_is_conditional')['when_parent_is']; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
foreach ($rules as $key => $rule) { |
33
|
|
|
if (in_array('required', $rule)) { |
34
|
|
|
$requiredKey = array_search('required', $rule); |
35
|
|
|
|
36
|
|
|
$rules[$key][$requiredKey] = 'required_if:' . $this->parent()->input_dot_name . '.selected,' . $requiredWhen; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $rules; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* All the Validation rules for this Input |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function errorMessages() { |
50
|
|
|
$messages = $this->validators->errorMessages(); |
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Rewrite required to required_if for items inside conditional selects |
54
|
|
|
*/ |
55
|
|
|
if ($this->parent() instanceof LsfConditionalSelect) { |
56
|
|
|
foreach ($messages as $key => $rule) { |
57
|
|
|
if (Str::endsWith($key, 'required')) { |
58
|
|
|
$messages[$key . '_if'] = $messages[$key]; |
59
|
|
|
|
60
|
|
|
unset($messages[$key]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $messages; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the Input’s response after the form has been submitted and validated |
70
|
|
|
* |
71
|
|
|
* @param $input |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function response($input) { |
75
|
|
|
return [ |
76
|
|
|
'label' => $this->label, |
|
|
|
|
77
|
|
|
'name' => $this->name, |
|
|
|
|
78
|
|
|
'response' => $input, |
79
|
|
|
'type' => $this->type, |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
} |