@@ -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 *
0 commit comments