Skip to content

Commit 77594cd

Browse files
authored
Merge pull request #143 from Laravel-Backpack/interactive-validation
Added `validation` option to `backpack:crud` command
2 parents ad4f3a4 + 31e3d82 commit 77594cd

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
],
2727
"require": {
28-
"backpack/crud": "^5.3.3"
28+
"backpack/crud": "^5.3.11"
2929
},
3030
"require-dev": {
3131
"phpunit/phpunit" : "^9.0||^7.0",

src/Console/Commands/CrudBackpackCommand.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class CrudBackpackCommand extends GeneratorCommand
1414
*
1515
* @var string
1616
*/
17-
protected $signature = 'backpack:crud {name}';
17+
protected $signature = 'backpack:crud {name}
18+
{--validation= : Validation type, must be request, array or field}';
1819

1920
/**
2021
* The console command description.
@@ -44,14 +45,22 @@ public function handle()
4445

4546
$this->infoBlock("Creating CRUD for the <fg=blue>$nameTitle</> model:");
4647

48+
// Validate validation option
49+
$validation = $this->handleValidationOption();
50+
if (! $validation) {
51+
return false;
52+
}
53+
4754
// Create the CRUD Model and show output
4855
$this->call('backpack:crud-model', ['name' => $nameTitle]);
4956

5057
// Create the CRUD Controller and show output
51-
$this->call('backpack:crud-controller', ['name' => $nameTitle]);
58+
$this->call('backpack:crud-controller', ['name' => $nameTitle, '--validation' => $validation]);
5259

5360
// Create the CRUD Request and show output
54-
$this->call('backpack:crud-request', ['name' => $nameTitle]);
61+
if ($validation === 'request') {
62+
$this->call('backpack:crud-request', ['name' => $nameTitle]);
63+
}
5564

5665
// Create the CRUD route
5766
$this->call('backpack:add-custom-route', [
@@ -76,6 +85,39 @@ public function handle()
7685
$this->newLine();
7786
}
7887

88+
/**
89+
* Handle validation Option.
90+
*
91+
* @return string
92+
*/
93+
private function handleValidationOption()
94+
{
95+
$options = ['request', 'array', 'field'];
96+
97+
// Validate validation option
98+
$validation = $this->option('validation');
99+
100+
if (! $validation) {
101+
$validation = $this->askHint(
102+
'How would you like to define your validation rules, for the Create and Update operations?', [
103+
'More info at <fg=blue>https://backpackforlaravel.com/docs/5.x/crud-operation-create#validation</>',
104+
'Valid options are <fg=blue>request</>, <fg=blue>array</> or <fg=blue>field</>',
105+
], $options[0]);
106+
107+
if (! $this->option('no-interaction')) {
108+
$this->deleteLines(5);
109+
}
110+
}
111+
112+
if (! in_array($validation, $options)) {
113+
$this->errorBlock("The validation must be request, array or field. '$validation' is not valid.");
114+
115+
return false;
116+
}
117+
118+
return $validation;
119+
}
120+
79121
/**
80122
* Get the stub file for the generator.
81123
*

src/Console/Commands/CrudControllerBackpackCommand.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class CrudControllerBackpackCommand extends GeneratorCommand
2222
*
2323
* @var string
2424
*/
25-
protected $signature = 'backpack:crud-controller {name}';
25+
protected $signature = 'backpack:crud-controller {name}
26+
{--validation=request : Validation type, must be request, array or field}';
2627

2728
/**
2829
* The console command description.
@@ -200,6 +201,30 @@ protected function replaceModel(&$stub, $name)
200201
return $this;
201202
}
202203

204+
/**
205+
* Replace the class name for the given stub.
206+
*
207+
* @param string $stub
208+
* @param string $name
209+
* @return string
210+
*/
211+
protected function replaceRequest(&$stub)
212+
{
213+
$validation = $this->option('validation');
214+
215+
// replace request class when validation is array
216+
if ($validation === 'array') {
217+
$stub = str_replace('DummyClassRequest::class', "[\n // 'name' => 'required|min:2',\n ]", $stub);
218+
}
219+
220+
// remove the validation class when validation is field
221+
if ($validation === 'field') {
222+
$stub = str_replace(" CRUD::setValidation(DummyClassRequest::class);\n\n", '', $stub);
223+
}
224+
225+
return $this;
226+
}
227+
203228
/**
204229
* Build the class with the given name.
205230
*
@@ -211,9 +236,10 @@ protected function buildClass($name)
211236
$stub = $this->files->get($this->getStub());
212237

213238
$this->replaceNamespace($stub, $name)
214-
->replaceNameStrings($stub, $name)
215-
->replaceModel($stub, $name)
216-
->replaceSetFromDb($stub, $name);
239+
->replaceRequest($stub)
240+
->replaceNameStrings($stub, $name)
241+
->replaceModel($stub, $name)
242+
->replaceSetFromDb($stub, $name);
217243

218244
return $stub;
219245
}

0 commit comments

Comments
 (0)