fhteam / laravel-validator
A bunch of Laravel validators
v2.4.0
2022-09-13 08:48 UTC
Requires
- php: ^8.1
Requires (Dev)
- giggsey/libphonenumber-for-php: ~7.0
- laravel/framework: ^9.0
- orchestra/testbench: ^7.0
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.0
Suggests
- giggsey/libphonenumber-for-php: Allows to validate phone numbers
README
Features:
- Validation logic is completely separate from objects being validated
- Validation rules and validation failure behaviour can be written in a declarative way in 90% of cases
- Uses Laravel validator core and rules
- Stateful model validation
- Provides OOP way to write and register new validation rules
- Serves as a replacer to a Input::get() to prevent unvalidated data from sneaking into your application
- Rule-based data transformations of validated data before it reaches your application logic (in progress)
- Provides unified way to write rules for controllers and models
- Can validate and aggregate many types of input: $_GET/$_POST, $_FILES, HTTP headers, Session data, Cookies (both PHP native and Laravel's encrypted ones)
- Two types of input validators: middleware (to be manually registered) and validate-when-resolved (just inject and you are done)
Documentation
- Documentation is available here
Quick example:
class OrderControllerValidator extends FrontendControllerValidatorWhenResolved { protected $rules = [ '*' => [ 'user_id' => 'required|numeric', ], 'getCreate, getShow, getEdit' => [], 'postOpen, postDelete, postAssignContractor, postCancelContractorAssignment' => [], 'postCreate' => [ 'title' => 'required|max:100', 'categoryId' => 'required|numeric', 'description' => 'required|max:4096', ], 'postEdit, postFileDelete, postFileUpload' => [ 'title' => 'required|max:100', 'categoryId' => 'required|numeric', 'description' => 'required|max:4096', ], ]; protected $errorRedirects = [ 'getShow' => ['route' => 'home'], 'postCreate' => ['route' => 'orders_create'], 'postEdit, postFileUpload, postFileDelete' => ['route' => ['orders_edit', ['orderId' => '#orderId']]], ]; } class OrderController extends Controller { /** * @var OrderControllerValidator */ protected $validator; /** * IoC invoked constructor */ public function __construct(OrderControllerValidator $validator) { $this->validator = $validator; } public function getShowValidatedData() { return Response::make($this->validator->description); }