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