PHP code example of choz / request-validation-bundle

1. Go to this page and download the library: Download choz/request-validation-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

choz / request-validation-bundle example snippets


// config/bundles.php

return [
    // ...
    Choz\RequestValidationBundle\ChozRequestValidationBundle::class => ['all' => true],
];



declare(strict_types=1);

namespace App\Request;

use Choz\RequestValidationBundle\Request\BaseRequest;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Constraints\Type;

class TagCreateRequest extends BaseRequest
{
    protected function rules(): array
    {
        return [
            new Collection([
                'id' => [new Required(), new Type('int')],
                'name' => [new Required(), new Type('string')],
            ]),
        ];
    }
}

 

namespace App\Controller;

use App\Request\TagCreateRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class TagCreateController extends AbstractController
{
    #[Route('/tags', methods: ['POST'])]
    public function __invoke(TagCreateRequest $request): JsonResponse {
        $id = $request->getInteger('id');
        $name = $request->getString('name');
        // use your values
        return new JsonResponse(['id' => $id, 'name' => $name], status: JsonResponse::HTTP_CREATED);
    }
}



declare(strict_types=1);

namespace App\Request;

use Choz\RequestValidationBundle\Request\BaseRequest;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Constraints\Type;

class TagCreateRequest extends BaseRequest
{
    protected function rules(): array
    {
        return [
            new Collection([
                'id' => [new Required(), new Type('int')],
                'name' => [new Required(), new Type('string')],
            ]),
        ];
    }

    public function getId(): int {
        // return $this->request()->getInt('id'); this works too.
        return $this->getInteger('id');
    }

    public function getName(): string {
        // return $this->request()->getAlpha('name'); this works too.
        return $this->getString('name');
    }
}

 

namespace App\Controller;

use App\Request\TagCreateRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class TagCreateController extends AbstractController
{
    #[Route('/tags', methods: ['POST'])]
    public function __invoke(TagCreateRequest $request): JsonResponse {
        $id = $request->getId();
        $name = $request->getName();
        // use your values
        return new JsonResponse(['id' => $id, 'name' => $name], status: JsonResponse::HTTP_CREATED);
    }
}
yaml
# config/packages/choz_request_validation.yaml
choz_request_validation:
    response_code: !php/const Symfony\Component\HttpFoundation\Response::HTTP_UNPROCESSABLE_ENTITY # 422