1
|
|
|
<?php namespace PascalKleindienst\FormListGenerator\Fields; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Field for Form generator |
5
|
|
|
* @package \PascalKleindienst\FormListGenerator\Fields |
6
|
|
|
*/ |
7
|
|
|
class Field extends AbstractField |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string Specify a placeholder for the input |
11
|
|
|
*/ |
12
|
|
|
public $placeholder = ''; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array Options used for a select tag |
16
|
|
|
*/ |
17
|
|
|
public $options = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var mixed Default value for the input element |
21
|
|
|
*/ |
22
|
|
|
public $default; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
*/ |
27
|
48 |
|
protected function registerConfigKeys() |
28
|
|
|
{ |
29
|
48 |
|
return [ 'placeholder', 'options' ]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
45 |
|
public function getValue(array $records = []) |
36
|
|
|
{ |
37
|
|
|
// Types |
38
|
45 |
|
$this->initInputType($this->getRecord($records)); |
39
|
|
|
|
40
|
|
|
// Attributes |
41
|
45 |
|
$this->setAttributes(); |
42
|
|
|
|
43
|
|
|
// Disabled |
44
|
45 |
|
if ($this->disabled) { |
45
|
3 |
|
$this->input->disable(); |
46
|
3 |
|
} |
47
|
|
|
|
48
|
|
|
// Readonly |
49
|
45 |
|
if ($this->readOnly) { |
50
|
3 |
|
$this->input->attribute('readonly', true); |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
// required |
54
|
45 |
|
$this->setRequired(' *'); |
55
|
|
|
|
56
|
|
|
// Placeholder |
57
|
45 |
|
if ($this->placeholder !== '') { |
58
|
3 |
|
$this->input->placeholder($this->placeholder); |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
// Label |
62
|
45 |
|
if (isset($this->config['comment'])) { |
63
|
3 |
|
$this->cssClass = str_replace('form-control', '', $this->cssClass); |
64
|
3 |
|
$this->input = $this->labelAfterInput(' ' . $this->config['comment']); |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
// Css |
68
|
45 |
|
$this->input->addClass($this->cssClass); |
69
|
|
|
|
70
|
45 |
|
return $this->builder->label($this->label)->addClass('d-block')->before($this->input); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Initialize the input field based on the type |
75
|
|
|
* |
76
|
|
|
* @param mixed $record |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
45 |
|
protected function initInputType($record) |
80
|
|
|
{ |
81
|
|
|
// init input |
82
|
45 |
|
switch ($this->type) { |
83
|
45 |
|
case 'password': |
84
|
6 |
|
$this->input = $this->builder->password($this->fieldName); |
85
|
3 |
|
break; |
86
|
42 |
|
case 'date': |
87
|
3 |
|
$this->input = $this->builder->date($this->fieldName); |
88
|
3 |
|
break; |
89
|
39 |
|
case 'email': |
90
|
3 |
|
$this->input = $this->builder->email($this->fieldName); |
91
|
3 |
|
break; |
92
|
36 |
|
case 'textarea': |
93
|
3 |
|
$this->input = $this->builder->textarea($this->fieldName); |
94
|
3 |
|
break; |
95
|
33 |
|
case 'dropdown': |
96
|
3 |
|
$this->input = $this->builder->select($this->fieldName, $this->getOptions())->select($record); |
97
|
3 |
|
break; |
98
|
30 |
|
case 'checkbox': |
99
|
6 |
|
$this->input = $this->builder->checkbox($this->fieldName); |
100
|
|
|
|
101
|
6 |
|
if ($this->default) { |
102
|
3 |
|
$this->input->check(); |
103
|
3 |
|
} |
104
|
|
|
|
105
|
6 |
|
$this->input->value($record); |
106
|
9 |
|
break; |
107
|
24 |
|
default: |
108
|
24 |
|
$this->input = $this->builder->text($this->fieldName); |
109
|
45 |
|
} |
110
|
|
|
|
111
|
|
|
// set value and default value |
112
|
45 |
|
if ($this->type === 'date') { |
113
|
3 |
|
$this->input->value(new \DateTime($record)); |
114
|
45 |
|
} elseif ($this->type !== 'dropdown' && $this->type !== 'checkbox') { |
115
|
33 |
|
$this->input->value($record)->defaultValue($this->default); |
116
|
33 |
|
} |
117
|
45 |
|
} |
118
|
|
|
} |
119
|
|
|
|