-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationFormTrait.php
More file actions
75 lines (59 loc) · 1.92 KB
/
Copy pathConfigurationFormTrait.php
File metadata and controls
75 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace ADT\FancyAdmin\UI\Components\Forms\Configuration;
use ADT\FancyAdmin\DI\Injects\EntityManagerInject;
use ADT\FancyAdmin\Model\Entities\Configuration;
use ADT\FancyAdmin\Model\Entities\Enums\ConfigurationTypeEnum;
use ADT\Forms\Form;
use App\Model\Entities\Account;
/**
* @property Account $entity
*/
trait ConfigurationFormTrait
{
use EntityManagerInject;
public function initForm(Form $form): void
{
$form->addText('key', 'Key')
->setRequired()
->setDisabled();
$form->addSelect('type', 'Type', ConfigurationTypeEnum::list())
->setRequired()
->setDisabled();
$form['type']
->addCondition(Form::Equal, ConfigurationTypeEnum::TYPE_SELECT)
->toggle('options-input');
$form->addTextArea('value', 'Value')
->setHtmlAttribute('rows', 5);
$form->addTextArea('options', 'Options')
->setHtmlAttribute('rows', 5);
$form->addSubmit("submit", 'Save');
}
public function validateForm(Configuration $entity, array $inputs): void
{
if ($entity->getType() === ConfigurationTypeEnum::TYPE_JSON->value && !is_array(json_decode($inputs['value'], true))) {
$this->form->addError('Input value not contains valid JSON');
}
if ($entity->getType() === ConfigurationTypeEnum::TYPE_SELECT->value) {
$options = json_decode($inputs['options'], true);
if (!is_array($options)) {
$this->form->addError('Input options not contains valid JSON');
return;
}
if ($inputs['value'] && !array_key_exists($inputs['value'], $options)) {
$this->form->addError('Input value not contains valid option from options input.');
}
}
}
public function processForm(Configuration $entity, array $inputs): void
{
if ($entity->getType() === ConfigurationTypeEnum::TYPE_JSON->value) {
$value = json_decode($inputs['value']);
$entity->setValue(json_encode($value, JSON_PRETTY_PRINT));
}
$this->_em->flush();
}
protected function getEntityClass(): ?string
{
return Configuration::class;
}
}