1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Formularium\Frontend\Vuelidate; |
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
|
|
|
// add extra classes |
35
|
|
|
$input = $previous->get('input'); |
36
|
|
|
if (count($input)) { |
37
|
|
|
$input[0]->setAttributes([ |
38
|
|
|
// TODO ':class' => 'status($v.text)', |
39
|
|
|
'v-model' => '$v.' . implode('.', $input[0]->getAttribute('v-model')) . '.$model' |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
foreach ($validators as $validator => $data) { |
44
|
|
|
switch ($validator) { |
45
|
|
|
case Datatype::REQUIRED: |
46
|
|
|
case Filled::class: |
47
|
|
|
$this->setValidations( |
48
|
|
|
$field, |
49
|
|
|
'required', |
50
|
|
|
'required' |
51
|
|
|
); |
52
|
|
|
break; |
53
|
|
|
case Equals::class: |
54
|
|
|
// TODO |
55
|
|
|
break; |
56
|
|
|
|
57
|
|
|
case In::class: |
58
|
|
|
// TODO |
59
|
|
|
break; |
60
|
|
|
|
61
|
|
|
case Max::class: |
62
|
|
|
$this->setValidations( |
63
|
|
|
$field, |
64
|
|
|
'maxValue', |
65
|
|
|
'maxValue(' . $field->getValidatorOption($validator, 'value', '') . ')' |
66
|
|
|
); |
67
|
|
|
break; |
68
|
|
|
case Min::class: |
69
|
|
|
$this->setValidations( |
70
|
|
|
$field, |
71
|
|
|
'minValue', |
72
|
|
|
'minValue(' . $field->getValidatorOption($validator, 'value', '') . ')' |
73
|
|
|
); |
74
|
|
|
break; |
75
|
|
|
|
76
|
|
|
case MaxLength::class: |
77
|
|
|
$this->setValidations( |
78
|
|
|
$field, |
79
|
|
|
'maxLength', |
80
|
|
|
'maxLength(' . $field->getValidatorOption($validator, 'value', '') . ')' |
81
|
|
|
); |
82
|
|
|
break; |
83
|
|
|
case MinLength::class: |
84
|
|
|
$this->setValidations( |
85
|
|
|
$field, |
86
|
|
|
'minLength', |
87
|
|
|
'minLength(' . $field->getValidatorOption($validator, 'value', '') . ')' |
88
|
|
|
); |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case NotIn::class: |
92
|
|
|
// TODO |
93
|
|
|
break; |
94
|
|
|
|
95
|
|
|
case Password::class: |
96
|
|
|
// TODO |
97
|
|
|
break; |
98
|
|
|
|
99
|
|
|
case Regex::class: |
100
|
|
|
$name = 'regex' . mt_rand(); |
101
|
|
|
$this->setValidations( |
102
|
|
|
$field, |
103
|
|
|
$name, |
104
|
|
|
'helpers.regex(\'' . $name . '\', /' . $field->getValidatorOption($validator, 'value', '') . '/)', |
105
|
|
|
'helpers' |
106
|
|
|
); |
107
|
|
|
break; |
108
|
|
|
|
109
|
|
|
case SameAs::class: |
110
|
|
|
$target = $field->getValidatorOption($validator, 'value', ''); |
111
|
|
|
$locator = $target;// TODO |
112
|
|
|
$this->setValidations( |
113
|
|
|
$field, |
114
|
|
|
'sameAs', |
115
|
|
|
'sameAs(' . $locator . ')' |
116
|
|
|
); |
117
|
|
|
break; |
118
|
|
|
default: |
119
|
|
|
break; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $previous; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function getVueCode(): VueCode |
127
|
|
|
{ |
128
|
|
|
/** |
129
|
|
|
* @var FrameworkVue $vue |
130
|
|
|
*/ |
131
|
|
|
$vue = $this->composer->getByName('Vue'); |
132
|
|
|
return $vue->getVueCode(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Sets validation |
137
|
|
|
* |
138
|
|
|
* @param Field $field |
139
|
|
|
* @param string $name |
140
|
|
|
* @param string $value |
141
|
|
|
* @param string $import |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
protected function setValidations(Field $field, $name, $value = null, $import = ''): void |
145
|
|
|
{ |
146
|
|
|
$vueCode = $this->getVueCode(); |
147
|
|
|
// TODO: does not work with in-file, only SFC |
148
|
|
|
$vueCode->appendImport($import ? $import : $name, 'vuelidate/lib/validators'); |
149
|
|
|
$other = &$vueCode->getOther(); |
150
|
|
|
$other['validations'][$field->getName()][$name] = $value; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|