blixit/symfony-request-validator-bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require relief_applications/symfony-request-validator-bundle @dev
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new RA\RequestValidatorBundle\RARequestValidatorBundle(),
);
}
}
# app/config/services.yml according to your project path
imports:
- { resource: "@RARequestValidatorBundle/Resources/config/services.yml" }
To test our default controller
# app/config/routing.yml according to your project path
RequestValidatorBundle:
resource: "@RARequestValidatorBundle/Resources/config/routing.yml"
this class should extend RA\RequestValidatorBundle\RequestValidator\Constraints. The list of constraints is available here https://symfony.com/doc/current/reference/constraints.html
<?php
namespace RequestValidatorBundle\Custom;
use RA\RequestValidatorBundle\RequestValidator\Constraints as RequestValidatorConstraints;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotEqualTo;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\Type;
/**
* TopicsConstraints
*
* A special class for topic constraints
*/
class TopicsConstraints extends RequestValidatorConstraints
{
protected function configure() : array
{
//Reusable rules can be store into variables starting by 'common'
$commonCategoryId = new Optional ([
new Type('numeric'),
new GreaterThan(0)
]);
$commonNotBlank = new NotBlank();
return [
'topics' => [
// rules for URI fields
'query' => new Collection([
'method' => new Optional ([new Choice(['by'])]),
'category_id' => $commonCategoryId
]),
// rules for POSTED fields
'request' => new Collection([
'category_id' => $commonCategoryId,
'title' => $commonNotBlank,
'description' => $commonNotBlank,
'message' => $commonNotBlank
]),
],
// rules for ANY fields
'topics2' => new Collection([
'category_id' => $commonCategoryId,
'title' => $commonNotBlank,
'description' => $commonNotBlank,
'message' => $commonNotBlank
]),
];
}
}
<?php
//...
use RequestValidatorBundle\Custom\TopicsConstraints; //our custom constraints class
use RA\RequestValidatorBundle\Annotations\ValidateQuery;
use RA\RequestValidatorBundle\Annotations\ValidateRequest;
class TopicController extends Controller
{
/**
* @Get("/", name="get_all_topics")
* @ValidateQuery( configuration="topics", constraintsClass=TopicsConstraints::class)
* Get a table of all the topics
*/
public function getAllAction(Request $request){
$categoryId = $request->get('category_id');
//...
}
/**
* @Post("/", name="create_topic")
* @ValidateRequest( configuration="topics", constraintsClass=TopicsConstraints::class)
* Get a table of all the topics
*/
public function createAction(Request $request){
$categoryId = $request->request->get('category_id');
$title = $request->request->get('title');
$description = $request->request->get('description');
$messageContent = $request->request->get('message');
//...
}
<?php
//...
use RequestValidatorBundle\Custom\TopicsConstraints; //our custom constraints class
use RA\RequestValidatorBundle\RequestValidator\ValidationException as RequestValidatorValidationException;
class TopicController extends Controller
{
/**
* @Get("/", name="get_all_topics")
* Get a table of all the topics
*/
public function getAllAction(Request $request){
try {
$validator = $this->get('request_validator');
$this->validator->validateQuery('topics',TopicsConstraints::class);
} catch (RequestValidatorValidationException $e) {
return new JsonResponse($e->getErrors(), $e->getCode());
}
$categoryId = $request->get('category_id');
}
/**
* @Post("/", name="create_topic")
* Get a table of all the topics
*/
public function createAction(Request $request){
try {
$validator = $this->get('request_validator');
$this->validator->validateRequest('topics',TopicsConstraints::class);
} catch (RequestValidatorValidationException $e) {
return new JsonResponse($e->getErrors(), $e->getCode());
}
$categoryId = $request->request->get('category_id');
$title = $request->request->get('title');
$description = $request->request->get('description');
$messageContent = $request->request->get('message');
}
This method check both, URI and posted parameters. When using ValidateAny, the configuration defined into the constraints class can only contain a constraints collection. Here we use topics2. ValidateAny can also be used as a function in the controller body. For now, ValidateAny will merge request fields and query fields if they have the same name.
<?php
//...
use RequestValidatorBundle\Custom\TopicsConstraints; //our custom constraints class
use RA\RequestValidatorBundle\Annotations\ValidateAny;
class TopicController extends Controller
{
/**
* @Get("/", name="get_all_topics")
* @ValidateAny( configuration="topics2", constraintsClass=TopicsConstraints::class)
* Get a table of all the topics
*/
public function getAllAction(Request $request){
$categoryId = $request->get('category_id');
//...
}
/**
* @Post("/", name="create_topic")
* @ValidateAny( configuration="topics2", constraintsClass=TopicsConstraints::class)
* Get a table of all the topics
*/
public function createAction(Request $request){
$categoryId = $request->request->get('category_id');
$title = $request->request->get('title');
$description = $request->request->get('description');
$messageContent = $request->request->get('message');
//...
}
How can I help you explore Laravel packages today?