This package provides annotations to easily add validation rules on action methods in a controller. Here's a quick example:
use Bakome\ValidationAttributes\Attributes\ValidationRule;
class ExampleController
{
#[ValidationRule('title', 'required|string')]
public function actionMethod()
{
}
}
This project exists to provide simple and cleaner way to validate request data on Laravel controllers. Using validation rules in action methods often complicate readability of the controller and adding validation in separate requests classes imply switching trough that classes multiple time to view and change the rules (Losing focus). With this simple attributes we can make validation more accessible and maintainable in the controllers without messing the controller code.
You can install the package via composer:
composer require bakome/laravel-validation-attributes
You can publish the config file with:
php artisan vendor:publish --provider="Bakome\ValidationAttributes\ValidationAttributesServiceProvider" --tag="config"
The package provides several annotations that should be put on controller action methods. These annotations are used to validate request data.
use Bakome\ValidationAttributes\Attributes\ValidationRule;
class ExampleController
{
#[ValidationRule('title', 'required|string')]
public function actionMethod()
{
}
}
This attribute will check if title parameter is present in current request and throw validation error if not present.
use Bakome\ValidationAttributes\Attributes\ValidationRule;
class ExampleController
{
#[ValidationRule('title', ['required', 'string'])]
public function actionMethod()
{
}
}
This attribute will check if title parameter is present in current request and throw validation error if not present.
use Bakome\ValidationAttributes\Attributes\ValidationRule;
class ExampleController
{
#[ValidationRule('description', 'required|string')]
#[ValidationRule('title', ['required', 'string'])]
public function actionMethod()
{
}
}
This attribute will check if title and description parameters are present in current request and throw validation error if not present.
use Bakome\ValidationAttributes\Attributes\ValidationRule;
use Bakome\ValidationAttributes\Attributes\ValidationRuleRedirect;
class ExampleController
{
#[ValidationRule('description', 'required|string')]
#[ValidationRule('title', ['required', 'string'])]
#[ValidationRuleRedirect('/a-redirection-route')]
public function actionMethod()
{
}
}
This attribute will check if title and description parameters are present in current request, compile validation error messages and redirect the page to provided route.
use Bakome\ValidationAttributes\Attributes\ValidationRule;
use Bakome\ValidationAttributes\Attributes\ValidationRuleRedirect;
class ExampleController
{
#[ValidationRule('description', 'required|string')]
#[ValidationRule('title', ['required', 'string'])]
#[ValidationRuleRedirect('/a-redirection-route', true)]
public function actionMethod()
{
}
}
This attribute will check if title and description parameters are present in current request, compile validation error messages and redirect the page to provided route including old input parameters.
'enabled' => true,
This plugin can be enabled or disabled with altering this property in configuration file. Default value is true (Enabled).
'middleware' => \Bakome\ValidationAttributes\Routing\Middleware\ValidationRulesByAttributes::class,
This property enables developers to provide a different handler for Validation via Middleware. Please be careful with this option and don't change it if you don't need custom behaviour.
'middleware' => \Bakome\ValidationAttributes\Routing\Middleware\ValidationRulesByAttributes::class,
This property enables developers to provide a different handler for Validation via Middleware. Please be careful with this option and don't change it if you don't need custom behaviour.
'api_pattern' => 'api/*',
This property provide value for detecting api routes. Using expects json sometimes is not enough and when that lacks this option/feature do the job.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
- Provide custom validation messages
- Provide validation groups to reduce boilerplate code