|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Formularium\Frontend\HTML\Renderable; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\Datatype; |
|
6
|
|
|
use Formularium\Datatype\Datatype_string; |
|
7
|
|
|
use Formularium\Field; |
|
8
|
|
|
use Formularium\Frontend\HTML\Framework; |
|
9
|
|
|
use Formularium\Frontend\HTML\Renderable; |
|
10
|
|
|
use Formularium\HTMLElement; |
|
11
|
|
|
|
|
12
|
|
|
class Renderable_string extends Renderable |
|
13
|
|
|
{ |
|
14
|
|
|
public const MAX_STRING_SIZE = 1024; |
|
15
|
|
|
|
|
16
|
|
|
use \Formularium\Frontend\HTML\RenderableViewableTrait; |
|
17
|
|
|
|
|
18
|
|
|
public function editable($value, Field $f, HTMLElement $previous): HTMLElement |
|
19
|
|
|
{ |
|
20
|
|
|
$input = new HTMLElement('input'); |
|
21
|
|
|
|
|
22
|
|
|
$extensions = $f->getExtensions(); |
|
23
|
|
|
$validators = $f->getValidators(); |
|
24
|
|
|
$input->setAttributes([ |
|
25
|
|
|
'id' => $f->getName() . Framework::counter(), |
|
26
|
|
|
'type' => ($extensions[static::HIDDEN] ?? false ? 'hidden' : 'text'), |
|
27
|
|
|
'name' => $f->getName(), |
|
28
|
|
|
'class' => '', |
|
29
|
|
|
'data-attribute' => $f->getName(), |
|
30
|
|
|
'data-datatype' => $f->getDatatype()->getName(), |
|
31
|
|
|
'data-basetype' => $f->getDatatype()->getBasetype(), |
|
32
|
|
|
'value' => $value, |
|
33
|
|
|
'maxlength' => static::MAX_STRING_SIZE, |
|
34
|
|
|
'title' => $f->getExtension(static::LABEL, '') |
|
35
|
|
|
]); |
|
36
|
|
|
|
|
37
|
|
|
if (isset($extensions[static::PLACEHOLDER])) { |
|
38
|
|
|
$input->setAttribute('placeholder', $extensions[static::PLACEHOLDER]); |
|
39
|
|
|
} |
|
40
|
|
|
if ($validators[Datatype::REQUIRED] ?? false) { |
|
41
|
|
|
$input->setAttribute('required', 'required'); |
|
42
|
|
|
} |
|
43
|
|
|
foreach ([static::DISABLED, static::READONLY] as $v) { |
|
44
|
|
|
if ($f->getExtension($v, false)) { |
|
45
|
|
|
$input->setAttribute($v, $v); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (array_key_exists(Datatype_string::MIN_LENGTH, $validators)) { |
|
50
|
|
|
$input->setAttribute('minlength', $validators[Datatype_string::MIN_LENGTH]); |
|
51
|
|
|
} |
|
52
|
|
|
if (array_key_exists(Datatype_string::MAX_LENGTH, $validators) |
|
53
|
|
|
&& $validators[Datatype_string::MAX_LENGTH] < static::MAX_STRING_SIZE // TODO: datatype |
|
54
|
|
|
) { |
|
55
|
|
|
$input->setAttribute('maxlength', $validators[Datatype_string::MAX_LENGTH]); |
|
56
|
|
|
} |
|
57
|
|
|
if (isset($extensions[static::NO_AUTOCOMPLETE])) { |
|
58
|
|
|
$input->setAttribute('autocomplete', 'off'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->container($input, $f); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|