1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms\Blocks; |
4
|
|
|
|
5
|
|
|
class LsfFieldset extends \Riclep\Storyblok\Block |
6
|
|
|
{ |
7
|
|
|
//// potentially all fields in a fieldset could be name <input name="fieldsetname[fieldname]"> |
8
|
|
|
/// this would out a multidimensional array in the response. |
9
|
|
|
/// makes validation herder? |
10
|
|
|
|
11
|
|
|
protected $inFieldSet = false; |
12
|
|
|
protected $isRepeating = false; |
13
|
|
|
|
14
|
|
|
public function __construct($content, $parent = null) |
15
|
|
|
{ |
16
|
|
|
parent::__construct($content, $parent); |
17
|
|
|
|
18
|
|
|
if ($this->parent() instanceof LsfFieldset) { |
19
|
|
|
$this->inFieldSet = true; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if ($this->parent() instanceof LsfRepeatingFieldset) { |
23
|
|
|
$this->isRepeating = true; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getInputNameAttribute() { |
28
|
|
|
if ($this->isRepeating) { |
29
|
|
|
return $this->parent()->input_name . '[' . $this->key . '][' . $this->content()['name'] . ']'; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($this->inFieldSet) { |
33
|
|
|
return $this->parent()->input_name . '[' . $this->content()['name'] . ']'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $this->content()['name']; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function response($input) { |
40
|
|
|
//dd($input, $this); |
41
|
|
|
|
42
|
|
|
return $this->fields->map(function ($field) use ($input) { |
|
|
|
|
43
|
|
|
|
44
|
|
|
// dump($field, $input); |
45
|
|
|
// TODO - handle radio buttons being empty |
46
|
|
|
dump($field, $input, $field->name); |
47
|
|
|
|
48
|
|
|
return $field->response($input[$field->name]); |
49
|
|
|
/*dd($field); |
50
|
|
|
|
51
|
|
|
return [ |
52
|
|
|
'label' => $field->label, |
53
|
|
|
'response' => $field->response($input[$field->name] ?? ''), |
54
|
|
|
];*/ |
55
|
|
|
})->toArray(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns all the validation rules for the fields in this Fieldset |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function validationRules() { |
65
|
|
|
$rules = []; |
66
|
|
|
|
67
|
|
|
$this->fields->each(function ($field) use (&$rules) { |
|
|
|
|
68
|
|
|
$rules = array_merge($rules, $field->validationRules()); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
return $rules; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns all the error messages for the fields in this Fieldset |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function errorMessages() { |
80
|
|
|
$rules = []; |
81
|
|
|
|
82
|
|
|
$this->fields->each(function ($field) use (&$rules) { |
|
|
|
|
83
|
|
|
$rules = array_merge($rules, $field->errorMessages()); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
return $rules; |
87
|
|
|
} |
88
|
|
|
} |