Skip to content

Commit c427cea

Browse files
committed
feat: validations support added in fields
1 parent 0a8b35e commit c427cea

File tree

2 files changed

+44
-46
lines changed

2 files changed

+44
-46
lines changed

src/Generators/Scaffold/ViewGenerator.php

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,10 @@ private function generateTableHeaderFields(): string
148148
continue;
149149
}
150150

151-
if ($this->config->options->localized) {
152-
/**
153-
* Replacing $FIELD_NAME$ before fill_template_with_field_data_locale() otherwise also
154-
* $FIELD_NAME$ get replaced with @lang('models/$modelName.fields.$value')
155-
* and so we don't have $FIELD_NAME$ in table_header_locale.stub
156-
* We could need 'raw' field name in header for example for sorting.
157-
* We still have $FIELD_NAME_TITLE$ replaced with @lang('models/$modelName.fields.$value').
158-
*
159-
* @see issue https://github.com/InfyOmLabs/laravel-generator/issues/887
160-
*/
161-
$preFilledHeaderFieldTemplate = str_replace('$FIELD_NAME$', $field->name, '');
162-
163-
$headerFields[] = fill_template_with_field_data_locale(
164-
$this->config->dynamicVars,
165-
['$FIELD_NAME_TITLE$' => 'fieldTitle', '$FIELD_NAME$' => 'name'],
166-
$preFilledHeaderFieldTemplate,
167-
$field
168-
);
169-
} else {
170-
$headerFields[] = view(
171-
$this->templateType.'::templates.scaffold.table.blade.header',
172-
$field->variables()
173-
)->render();
174-
}
151+
$headerFields[] = view(
152+
$this->templateType.'::templates.scaffold.table.blade.header',
153+
$field->variables()
154+
)->render();
175155
}
176156

177157
return implode(infy_nl_tab(1, 4), $headerFields);
@@ -210,27 +190,6 @@ private function generateFields()
210190
continue;
211191
}
212192

213-
$validations = explode('|', $field->validations);
214-
$minMaxRules = '';
215-
foreach ($validations as $validation) {
216-
if (!Str::contains($validation, ['max:', 'min:'])) {
217-
continue;
218-
}
219-
220-
$validationText = substr($validation, 0, 3);
221-
$sizeInNumber = substr($validation, 4);
222-
223-
$sizeText = ($validationText == 'min') ? 'minlength' : 'maxlength';
224-
if ($field->htmlType == 'number') {
225-
$sizeText = $validationText;
226-
}
227-
228-
$size = ",'$sizeText' => $sizeInNumber";
229-
$minMaxRules .= $size;
230-
}
231-
// TODO:
232-
// $this->config->addDynamicVariable('$SIZE$', $minMaxRules);
233-
234193
$htmlFields[] = HTMLFieldGenerator::generateHTML(
235194
$field,
236195
$this->templateViewPath

src/Utils/HTMLFieldGenerator.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace InfyOm\Generator\Utils;
44

5+
use Illuminate\Support\Str;
56
use InfyOm\Generator\Common\GeneratorField;
67

78
class HTMLFieldGenerator
@@ -11,6 +12,10 @@ public static function generateHTML(GeneratorField $field, $templateType): strin
1112
$viewName = $field->htmlType;
1213
$variables = [];
1314

15+
if (!empty($validations = self::generateValidations($field))) {
16+
$variables['options'] = ', '.implode(', ', $validations);
17+
}
18+
1419
switch ($field->htmlType) {
1520
case 'select':
1621
case 'enum':
@@ -43,7 +48,10 @@ public static function generateHTML(GeneratorField $field, $templateType): strin
4348

4449
return view($templateType.'.fields.radio_group', array_merge(
4550
['radioButtons' => implode(infy_nl(), $radioButtons)],
46-
$field->variables()
51+
array_merge(
52+
$field->variables(),
53+
$variables
54+
)
4755
))->render();
4856
}
4957

@@ -55,4 +63,35 @@ public static function generateHTML(GeneratorField $field, $templateType): strin
5563
)
5664
)->render();
5765
}
66+
67+
public static function generateValidations(GeneratorField $field)
68+
{
69+
$validations = explode('|', $field->validations);
70+
$validationRules = [];
71+
72+
foreach ($validations as $validation) {
73+
74+
if ($validation === 'required') {
75+
$validationRules[] = "'required'";
76+
continue;
77+
}
78+
79+
if (!Str::contains($validation, ['max:', 'min:'])) {
80+
continue;
81+
}
82+
83+
$validationText = substr($validation, 0, 3);
84+
$sizeInNumber = substr($validation, 4);
85+
86+
$sizeText = ($validationText == 'min') ? 'minlength' : 'maxlength';
87+
if ($field->htmlType == 'number') {
88+
$sizeText = $validationText;
89+
}
90+
91+
$size = "'$sizeText' => $sizeInNumber";
92+
$validationRules[] = $size;
93+
}
94+
95+
return $validationRules;
96+
}
5897
}

0 commit comments

Comments
 (0)