Skip to content

Commit

Permalink
Form validation added
Browse files Browse the repository at this point in the history
  • Loading branch information
sohelamin committed Dec 2, 2015
1 parent 47a4a37 commit c3ac70b
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ php artisan crud:generate Person --fields="name:string, email:string, age:number
You can also easily include route, set primary key, set views directory etc through options **--route**, **--pk**, **--view-path** as bellows:
```
php artisan crud:generate Person --fields="name:string, email:string, age:number, message:text" --route=yes --pk=id --view-path="admin" --namespace=Admin
php artisan crud:generate Person --fields="name:string:required, email:string, age:number, message:text:required" --route=yes --pk=id --view-path="admin" --namespace=Admin
```
-----------
Expand Down
23 changes: 18 additions & 5 deletions src/Commands/CrudCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,27 @@ public function handle()
$primaryKey = $this->option('pk');
$viewPath = $this->option('view-path');

$fillableArray = explode(',', $fields);
foreach ($fillableArray as $value) {
$data[] = preg_replace("/(.*?):(.*)/", "$1", trim($value));
$fieldsArray = explode(',', $fields);
$requiredFields = '';
$requiredFieldsStr = '';

foreach ($fieldsArray as $item) {
$fillableArray[] = preg_replace("/(.*?):(.*)/", "$1", trim($item));

$itemArray = explode(':', $item);
$currentField = trim($itemArray[0]);
$requiredFieldsStr .= (isset($itemArray[2])
&& (trim($itemArray[2]) == 'req'
|| trim($itemArray[2]) == 'required'))
? "'$currentField' => 'required', " : '';
}

$commaSeparetedString = implode("', '", $data);
$commaSeparetedString = implode("', '", $fillableArray);
$fillable = "['" . $commaSeparetedString . "']";

$this->call('crud:controller', ['name' => $controllerNamespace . $name . 'Controller', '--crud-name' => $name, '--view-path' => $viewPath]);
$requiredFields = ($requiredFieldsStr != '') ? "[" . $requiredFieldsStr . "]" : '';

$this->call('crud:controller', ['name' => $controllerNamespace . $name . 'Controller', '--crud-name' => $name, '--view-path' => $viewPath, '--required-fields' => $requiredFields]);
$this->call('crud:model', ['name' => $name, '--fillable' => $fillable, '--table' => str_plural(strtolower($name))]);
$this->call('crud:migration', ['name' => str_plural(strtolower($name)), '--schema' => $fields, '--pk' => $primaryKey]);
$this->call('crud:view', ['name' => $name, '--fields' => $fields, '--view-path' => $viewPath]);
Expand All @@ -83,4 +95,5 @@ public function handle()
}
}
}

}
25 changes: 24 additions & 1 deletion src/Commands/CrudControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class CrudControllerCommand extends GeneratorCommand
protected $signature = 'crud:controller
{name : The name of the controler.}
{--crud-name= : The name of the Crud.}
{--view-path= : The name of the view path.}';
{--view-path= : The name of the view path.}
{--required-fields=null : Required fields for validations.}';

/**
* The console command description.
Expand Down Expand Up @@ -68,13 +69,19 @@ protected function buildClass($name)
$crudNamePluralCap = str_plural($crudNameCap);
$crudNameSingular = str_singular($crudName);

$validationRules = '';
if ($this->option('required-fields') != '') {
$validationRules = "\$this->validate(\$request, " . $this->option('required-fields') . ");\n";
}

return $this->replaceNamespace($stub, $name)
->replaceViewPath($stub, $viewPath)
->replaceCrudName($stub, $crudName)
->replaceCrudNameCap($stub, $crudNameCap)
->replaceCrudNamePlural($stub, $crudNamePlural)
->replaceCrudNamePluralCap($stub, $crudNamePluralCap)
->replaceCrudNameSingular($stub, $crudNameSingular)
->replaceValidationRules($stub, $validationRules)
->replaceClass($stub, $name);
}

Expand Down Expand Up @@ -167,4 +174,20 @@ protected function replaceCrudNameSingular(&$stub, $crudNameSingular)

return $this;
}

/**
* Replace the validationRules for the given stub.
*
* @param string $stub
* @return $this
*/
protected function replaceValidationRules(&$stub, $validationRules)
{
$stub = str_replace(
'{{validationRules}}', $validationRules, $stub
);

return $this;
}

}
1 change: 1 addition & 0 deletions src/Commands/CrudMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,5 @@ protected function replaceSchemaDown(&$stub, $schemaDown)

return $this;
}

}
1 change: 1 addition & 0 deletions src/Commands/CrudModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ protected function replaceFillable(&$stub, $fillable)

return $this;
}

}
3 changes: 2 additions & 1 deletion src/Commands/CrudViewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function wrapField($item, $field)
%3\$s
{!! \$errors->first('%1\$s', '<p class="help-block">:message</p>') !!}
</div>
</div>
</div>\n
EOD;

return sprintf($formGroup, $item['name'], ucwords(strtolower(str_replace('_', ' ', $item['name']))), $field);
Expand Down Expand Up @@ -314,4 +314,5 @@ protected function createRadioField($item)

return $this->wrapField($item, sprintf($field, $item['name']));
}

}
1 change: 1 addition & 0 deletions src/CrudGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public function register()
'Appzcoder\CrudGenerator\Commands\CrudViewCommand'
);
}

}
4 changes: 2 additions & 2 deletions src/stubs/controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DummyClass extends Controller
*/
public function store(Request $request)
{
//$this->validate($request, ['name' => 'required']); // Uncomment and modify if you need to validate any input.
{{validationRules}}
{{crudNameCap}}::create($request->all());

Session::flash('flash_message', '{{crudNameCap}} successfully added!');
Expand Down Expand Up @@ -84,7 +84,7 @@ class DummyClass extends Controller
*/
public function update($id, Request $request)
{
//$this->validate($request, ['name' => 'required']); // Uncomment and modify if you need to validate any input.
{{validationRules}}
${{crudNameSingular}} = {{crudNameCap}}::findOrFail($id);
${{crudNameSingular}}->update($request->all());

Expand Down
1 change: 1 addition & 0 deletions src/stubs/migration.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ class DummyClass extends Migration
{
{{schema_down}}
}

}
2 changes: 1 addition & 1 deletion src/stubs/model.stub
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

namespace DummyNamespace;

Expand Down

0 comments on commit c3ac70b

Please sign in to comment.