1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Formularium\Frontend\VeeValidate; |
4
|
|
|
|
5
|
|
|
use Formularium\Datatype; |
6
|
|
|
use Formularium\Extradata; |
7
|
|
|
use Formularium\Field; |
8
|
|
|
use Formularium\Frontend\Vue\Framework as FrameworkVue; |
9
|
|
|
use Formularium\Frontend\Vue\VueCode; |
10
|
|
|
use Formularium\HTMLNode; |
11
|
|
|
use Formularium\Validator\Equals; |
12
|
|
|
use Formularium\Validator\Filled; |
13
|
|
|
use Formularium\Validator\In; |
14
|
|
|
use Formularium\Validator\Max; |
15
|
|
|
use Formularium\Validator\MaxLength; |
16
|
|
|
use Formularium\Validator\Min; |
17
|
|
|
use Formularium\Validator\MinLength; |
18
|
|
|
use Formularium\Validator\NotIn; |
19
|
|
|
use Formularium\Validator\Password; |
20
|
|
|
use Formularium\Validator\Regex; |
21
|
|
|
use Formularium\Validator\SameAs; |
22
|
|
|
|
23
|
|
|
class Renderable extends \Formularium\Renderable |
24
|
|
|
{ |
25
|
|
|
public function viewable($value, Field $field, HTMLNode $previous): HTMLNode |
26
|
|
|
{ |
27
|
|
|
return $previous; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function editable($value, Field $field, HTMLNode $previous): HTMLNode |
31
|
|
|
{ |
32
|
|
|
$validators = $field->getValidators(); |
33
|
|
|
|
34
|
|
|
// get the tag |
35
|
|
|
/** |
36
|
|
|
* @var HTMLNode|null $validationNode |
37
|
|
|
*/ |
38
|
|
|
$validationNode = null; |
39
|
|
|
$input = $previous->get('input'); |
40
|
|
|
if (count($input)) { |
41
|
|
|
$validationNode = $input[0]; |
42
|
|
|
} |
43
|
|
|
if (!$validationNode) { |
44
|
|
|
$input = $previous->get('textarea'); |
45
|
|
|
if (count($input)) { |
46
|
|
|
$validationNode = $input[0]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
if (!$validationNode) { |
50
|
|
|
$input = $previous->get('select'); |
51
|
|
|
if (count($input)) { |
52
|
|
|
$validationNode = $input[0]; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
if (!$validationNode) { |
56
|
|
|
return $previous; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// add rules |
60
|
|
|
$newInput = clone $validationNode; |
61
|
|
|
$validationNode->setTag('validation-provider'); |
62
|
|
|
$validationNode->clearAttributes()->clearContent(); |
63
|
|
|
$validationNode->addAttribute('v-slot', '{ errors }') |
64
|
|
|
->addContent([ |
65
|
|
|
$newInput, |
66
|
|
|
new HTMLNode('span', [], '{{ v.errors[0] }}') |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
$rules = []; |
70
|
|
|
foreach ($validators as $validator => $data) { |
71
|
|
|
switch ($validator) { |
72
|
|
|
case Datatype::REQUIRED: |
73
|
|
|
case Filled::class: |
74
|
|
|
$rules['required'] = true; |
75
|
|
|
break; |
76
|
|
|
|
77
|
|
|
case Equals::class: |
78
|
|
|
$rules['oneOf'] = implode(',', $field->getValidatorOption($validator, 'value', '')); |
|
|
|
|
79
|
|
|
break; |
80
|
|
|
|
81
|
|
|
case In::class: |
82
|
|
|
$rules['oneOf'] = implode(',', $field->getValidatorOption($validator, 'value', '')); |
83
|
|
|
break; |
84
|
|
|
|
85
|
|
|
case Max::class: |
86
|
|
|
$rules['max_value'] = $field->getValidatorOption($validator, 'value', ''); |
87
|
|
|
break; |
88
|
|
|
case Min::class: |
89
|
|
|
$rules['min_value'] = $field->getValidatorOption($validator, 'value', ''); |
90
|
|
|
break; |
91
|
|
|
|
92
|
|
|
case MaxLength::class: |
93
|
|
|
$rules['max'] = $field->getValidatorOption($validator, 'value', ''); |
94
|
|
|
break; |
95
|
|
|
case MinLength::class: |
96
|
|
|
$rules['min'] = $field->getValidatorOption($validator, 'value', ''); |
97
|
|
|
break; |
98
|
|
|
|
99
|
|
|
case NotIn::class: |
100
|
|
|
// TODO |
101
|
|
|
break; |
102
|
|
|
|
103
|
|
|
case Password::class: |
104
|
|
|
// TODO |
105
|
|
|
break; |
106
|
|
|
|
107
|
|
|
case Regex::class: |
108
|
|
|
$rules['regex'] = $field->getValidatorOption($validator, 'value', ''); |
109
|
|
|
break; |
110
|
|
|
|
111
|
|
|
case SameAs::class: |
112
|
|
|
// TODO |
113
|
|
|
break; |
114
|
|
|
default: |
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
$validationNode->addAttribute( |
119
|
|
|
'rules', |
120
|
|
|
json_encode(array_merge($rules, $this->rules($value, $field, $newInput))) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return $previous; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Other rules to add |
128
|
|
|
* |
129
|
|
|
* @param mixed $value |
130
|
|
|
* @param Field $field |
131
|
|
|
* @param HTMLNode $input |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
protected function rules($value, Field $field, HTMLNode $input): array |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
return []; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|