nobelatunje / laravel-validator
A simple and easier validation functionality built on top of Laravel Validator class.
dev-main
2022-05-08 02:00 UTC
Requires
- php: >=8.0.2
- illuminate/http: ^9.11
- illuminate/support: ^9.11
- illuminate/validation: ^9.11
This package is auto-updated.
Last update: 2025-01-08 08:07:37 UTC
README
A simple and easier validation functionality built on top of Laravel Validator class.
This library is majorly for Lumen but can also plugged into a Laravel application and easily manage validation.
Core functionalities are:
- Validates incoming request data and returns an array of validated parameters
- It can also return a model object if you like, just set the model parameters
- You can also extract validated fields even if the validation fails
- It can be injected into your controller class and usage is smooth
How to install
Install via composer
$ composer install nobelatunje/laravel-validator
How to use in your controller
namespace App\Http\Controllers; use Nobelatunje\LaravelValidator; class CustomerController extends Controller { public function create(LaravelValidator $validator, Request $request) { //set validation rules $this->setValidationRules($validator); if($validator->validate($request)) { //save the customer $validator->customer->save(); //do other things } else { //return the error message or do something else return $validator->error_message; } } private function setValidationRules(LaravelValidator $validator) { //set the model params $validator->setModelParams(Customer::class, 'customer') //set the fields and the validation rules ->field('firstname', 'string|required', 'FirstName') ->field('lastname', 'string|required', 'LastName') ->field('email', 'required|email:rfc,dns|unique:customers', 'Email Address') ->field('phone_number', 'required|string|unique:customers', 'Phone Number'); } }