moraesgil / entity-validator-trait
A simple Laravel trait for call Validation by your entitie to allow data creating or updating.
v1.0.0
2018-09-27 18:20 UTC
This package is auto-updated.
Last update: 2024-04-28 06:16:32 UTC
README
A simple Laravel trait for call Validation by your entities to allow data creating or updating.
Installation
With Composer
$ composer require moraesgil/entity-validator-trait
{ "require": { "moraesgil/entity-validator-trait": "~1.0" } }
Sample
... //Your Model use Traits\Entities\EntityValidatorTrait; //<<<<< add this line class YourLaravelEntity extends Model { use EntityValidatorTrait; protected $table = 'samples'; protected $rules = [ //<<<<< add this array with validation 'id' => 'required|max:80|unique:samples,id,@except,id', 'label' => 'required|max:80|unique:samples,label,@except,label',//<<<<< @except will be replaced by id value ]; }
Now you can call validate in your controller before store or update
// Your Controller class SampleController extends Controller { public function store(Request $request) { $model = new YourLaravelEntity(); //here is the magic if ($model->hasRules()) { //<<<<< check has rules if (!$model->validate($request->all())) { //<<<<< validate return response()->json($model->errors, 400); } } return response()->json($model->create($request->all()), 201); } public function update(Request $request) { //here is the magic if ($model->hasRules()) { //<<<<< check has rules if (!$model->validate($request->all(), $id)) { //<<<<< validate return response()->json($model->errors, 400); } } return response()->json($model->update($request->all()), 201); } }
Authors
- Gilberto Prudêncio Vaz de Moraes - Initial work - MoraesGil
License
This project is licensed under the MIT License - see the LICENSE file for details