Skip to content

Commit c841937

Browse files
committed
[EH] add filling validation rules from migration schema
1 parent 31aec12 commit c841937

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

config/generators.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
* have to implement Webfactor\Laravel\Generators\Contracts\MakeServiceInterface.
77
*/
88
'services' => [
9-
/*Webfactor\Laravel\Generators\Services\MigrationService::class,*/
9+
Webfactor\Laravel\Generators\Services\MigrationService::class,
1010
Webfactor\Laravel\Generators\Services\LanguageService::class,
1111
Webfactor\Laravel\Generators\Services\BackpackCrudModelService::class,
12-
/*Webfactor\Laravel\Generators\Services\BackpackCrudControllerService::class,
12+
Webfactor\Laravel\Generators\Services\BackpackCrudControllerService::class,
1313
Webfactor\Laravel\Generators\Services\BackpackCrudRequestService::class,
1414
Webfactor\Laravel\Generators\Services\FactoryService::class,
1515
Webfactor\Laravel\Generators\Services\SeederService::class,
16-
Webfactor\Laravel\Generators\Services\RouteService::class,*/
16+
Webfactor\Laravel\Generators\Services\RouteService::class,
1717
Webfactor\Laravel\Generators\Services\OpenIdeService::class,
1818
],
1919

src/MigrationField.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,26 @@ public function isUnique(): bool
9393
{
9494
return $this->unique;
9595
}
96+
97+
public function makeValidationRule(): string
98+
{
99+
$rule = 'required';
100+
101+
switch ($this->getType()) {
102+
103+
case 'string':
104+
$rule .= '|between:3,255';
105+
break;
106+
107+
case 'integer':
108+
$rule .= '|integer';
109+
break;
110+
111+
case 'date':
112+
$rule .= '|date';
113+
break;
114+
}
115+
116+
return $rule;
117+
}
96118
}

src/Services/BackpackCrudRequestService.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,43 @@ class BackpackCrudRequestService extends ServiceAbstract implements ServiceInter
99
{
1010
protected $relativeToBasePath = 'app/Http/Requests/Admin';
1111

12+
private $rules;
13+
1214
public function call()
1315
{
1416
$this->command->call('make:crud-request', [
1517
'name' => $this->getName($this->command->entity),
1618
]);
1719

1820
$this->addLatestFileToIdeStack();
21+
$this->fillRulesInGeneratedRequestFromSchema();
1922
}
2023

2124
public function getName(string $entity): string
2225
{
2326
return ucfirst($entity);
2427
}
28+
29+
private function fillRulesInGeneratedRequestFromSchema()
30+
{
31+
$requestFile = end($this->command->filesToBeOpened);
32+
33+
$model = $this->filesystem->get($requestFile);
34+
$model = str_replace('__rules__', $this->getRulesFromSchema(), $model);
35+
$this->filesystem->put($requestFile, $model);
36+
}
37+
38+
/**
39+
* @return string
40+
*/
41+
private function getRulesFromSchema()
42+
{
43+
$this->command->schema->getStructure()->each(function ($field) {
44+
if (!$field->isNullable()) {
45+
$this->rules .= "'" . $field->getName() . "' => '" . $field->makeValidationRule() . "',\n";
46+
}
47+
});
48+
49+
return $this->rules;
50+
}
2551
}

stubs/crud-request.stub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class DummyClassRequest extends FormRequest
2626
public function rules()
2727
{
2828
return [
29-
// 'name' => 'required|min:5|max:255'
30-
];
29+
__rules__ ];
3130
}
3231

3332
/**

0 commit comments

Comments
 (0)