|
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 mixed Default value for the input element |
|
16
|
|
|
*/ |
|
17
|
|
|
public $default; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritDoc} |
|
21
|
|
|
*/ |
|
22
|
72 |
|
protected function registerConfigKeys() |
|
23
|
|
|
{ |
|
24
|
72 |
|
return [ 'placeholder' ]; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritDoc} |
|
29
|
|
|
*/ |
|
30
|
63 |
|
public function getValue(array $records = []) |
|
31
|
|
|
{ |
|
32
|
|
|
// Types |
|
33
|
63 |
|
$record = $this->getRecord($records); |
|
34
|
63 |
|
$this->initInputType($record); |
|
35
|
|
|
|
|
36
|
|
|
// Attributes |
|
37
|
63 |
|
$this->setAttributes(); |
|
38
|
|
|
|
|
39
|
|
|
// Disabled |
|
40
|
63 |
|
if ($this->disabled) { |
|
41
|
3 |
|
$this->input->disable(); |
|
42
|
3 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Readonly |
|
45
|
63 |
|
if ($this->readOnly) { |
|
46
|
3 |
|
$this->input->attribute('readonly', true); |
|
47
|
3 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
// required |
|
50
|
63 |
|
$this->setRequired(' *'); |
|
51
|
|
|
|
|
52
|
|
|
// Placeholder |
|
53
|
63 |
|
if ($this->placeholder !== '') { |
|
54
|
12 |
|
$this->input->placeholder($this->placeholder); |
|
55
|
12 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Label |
|
58
|
63 |
|
if (isset($this->config['comment'])) { |
|
59
|
12 |
|
$this->cssClass = str_replace('form-control', '', $this->cssClass); |
|
60
|
12 |
|
$this->input = $this->labelAfterInput(' ' . $this->config['comment']); |
|
61
|
12 |
|
} |
|
62
|
|
|
|
|
63
|
63 |
|
if ($this->type === 'image' && isset($record)) { |
|
64
|
3 |
|
$this->label .= '<br/> <img src="' . $record . '" class="preview_' . $this->fieldName . '" />'; |
|
65
|
3 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Css |
|
68
|
63 |
|
$this->input->addClass($this->cssClass); |
|
69
|
|
|
|
|
70
|
63 |
|
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
|
63 |
|
protected function initInputType($record) |
|
80
|
|
|
{ |
|
81
|
|
|
// init input |
|
82
|
63 |
|
switch ($this->type) { |
|
83
|
63 |
|
case 'password': |
|
84
|
6 |
|
$this->input = $this->builder->password($this->fieldName); |
|
85
|
3 |
|
break; |
|
86
|
60 |
|
case 'date': |
|
87
|
3 |
|
$this->input = $this->builder->date($this->fieldName); |
|
88
|
3 |
|
break; |
|
89
|
57 |
|
case 'email': |
|
90
|
3 |
|
$this->input = $this->builder->email($this->fieldName); |
|
91
|
3 |
|
break; |
|
92
|
54 |
|
case 'textarea': |
|
93
|
3 |
|
$this->input = $this->builder->textarea($this->fieldName); |
|
94
|
3 |
|
break; |
|
95
|
51 |
|
case 'dropdown': |
|
96
|
12 |
|
$this->input = $this->builder->select($this->fieldName, $this->getOptions())->select($record); |
|
97
|
12 |
|
break; |
|
98
|
48 |
|
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
|
42 |
|
case 'number': |
|
108
|
3 |
|
$this->input = $this->builder->text($this->fieldName)->attribute('type', 'number'); |
|
109
|
3 |
|
break; |
|
110
|
39 |
|
case 'file': |
|
111
|
3 |
|
$this->input = $this->builder->file($this->fieldName); |
|
112
|
3 |
|
break; |
|
113
|
36 |
|
case 'image': |
|
114
|
3 |
|
$this->input = $this->builder->file($this->fieldName)->attribute('accept', 'image/*'); |
|
115
|
3 |
|
break; |
|
116
|
33 |
|
default: |
|
117
|
33 |
|
$this->input = $this->builder->text($this->fieldName); |
|
118
|
63 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
// set value and default value |
|
121
|
63 |
|
if ($this->type === 'date') { |
|
122
|
3 |
|
$this->input->value(new \DateTime($record)); |
|
123
|
63 |
|
} elseif ($this->type !== 'dropdown' && $this->type !== 'checkbox') { |
|
124
|
51 |
|
$this->input->value($record)->defaultValue($this->default); |
|
125
|
51 |
|
} |
|
126
|
63 |
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|