1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms; |
4
|
|
|
|
5
|
|
|
use Riclep\StoryblokForms\Blocks\LsfFieldset; |
6
|
|
|
use Riclep\StoryblokForms\Blocks\LsfRepeatingFieldset; |
7
|
|
|
use Riclep\StoryblokForms\Traits\ToJson; |
8
|
|
|
|
9
|
|
|
class Input extends \Riclep\Storyblok\Block |
10
|
|
|
{ |
11
|
|
|
use ToJson; |
12
|
|
|
|
13
|
|
|
protected $inFieldSet = false; |
14
|
|
|
protected $isRepeating = false; |
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 __construct($content, $parent = null) |
23
|
|
|
{ |
24
|
|
|
parent::__construct($content, $parent); |
25
|
|
|
|
26
|
|
|
if ($this->parent() instanceof LsfFieldset) { |
27
|
|
|
$this->inFieldSet = true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($this->parent() instanceof LsfRepeatingFieldset) { |
31
|
|
|
$this->isRepeating = true; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getInputNameAttribute() { |
36
|
|
|
if ($this->isRepeating) { |
37
|
|
|
return $this->parent()->input_name . '[' . $this->key . '][' . $this->content()['name'] . ']'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($this->inFieldSet) { |
41
|
|
|
return $this->parent()->input_name . '[' . $this->content()['name'] . ']'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->content()['name']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
public function response($input) { |
49
|
|
|
//dd($input, $this); |
50
|
|
|
|
51
|
|
|
return $this->fields->map(function ($field) use ($input) { |
|
|
|
|
52
|
|
|
|
53
|
|
|
// dump($field, $input); |
54
|
|
|
// TODO - handle radio buttons being empty |
55
|
|
|
dump($field, $input, $field->name); |
56
|
|
|
|
57
|
|
|
return $field->response($input[$field->name]); |
58
|
|
|
/*dd($field); |
59
|
|
|
|
60
|
|
|
return [ |
61
|
|
|
'label' => $field->label, |
62
|
|
|
'response' => $field->response($input[$field->name] ?? ''), |
63
|
|
|
];*/ |
64
|
|
|
})->toArray(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function loopKey($key) { |
68
|
|
|
$this->key = $key; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* All the Validation rules for this Input |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function validationRules() { |
80
|
|
|
return $this->validators->validationRules(); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* All the error messages for this Input |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function errorMessages() { |
89
|
|
|
return $this->validators->errorMessages(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |