项目作者: adamsafr

项目描述 :
This is an adaptation of the Laravel Form Request for Symfony
高级语言: PHP
项目地址: git://github.com/adamsafr/form-request-bundle.git
创建时间: 2019-03-27T14:47:36Z
项目社区:https://github.com/adamsafr/form-request-bundle

开源协议:MIT License

下载


Build Status

Form Request Bundle

This bundle provides similar solution as the Laravel Form Request.
Form request is custom request class that contains validation logic and
it’s executed (validated) before the controller action is called.

Installation

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

  1. $ composer require adamsafr/form-request-bundle

Applications that don’t use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

  1. $ composer require adamsafr/form-request-bundle

This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:

  1. // app/AppKernel.php
  2. // ...
  3. class AppKernel extends Kernel
  4. {
  5. public function registerBundles()
  6. {
  7. $bundles = [
  8. // ...
  9. new Adamsafr\FormRequestBundle\AdamsafrFormRequestBundle(),
  10. ];
  11. // ...
  12. }
  13. // ...
  14. }

Configuration

Create the adamsafr_form_request.yaml file in the config/packages directory
for Symfony 4 or add it in the app/config/config.yml file:

  1. adamsafr_form_request:
  2. exception_listeners:
  3. access_denied:
  4. # Sets json response of the Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
  5. enabled: true
  6. form_validation:
  7. # Sets json response of the Adamsafr\FormRequestBundle\Exception\FormValidationException
  8. enabled: true
  9. json_decode:
  10. # Sets json response of the Adamsafr\FormRequestBundle\Exception\JsonDecodeException
  11. enabled: true

Usage

  1. // src/Request/UserRequest.php
  2. namespace App\Request;
  3. use Adamsafr\FormRequestBundle\Http\FormRequest;
  4. use App\Service\Randomizer;
  5. use Symfony\Component\Validator\Constraint;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. class UserRequest extends FormRequest
  8. {
  9. /**
  10. * @var Randomizer
  11. */
  12. private $randomizer;
  13. /**
  14. * You can inject services here
  15. *
  16. * @param Randomizer $randomizer
  17. */
  18. public function __construct(Randomizer $randomizer)
  19. {
  20. $this->randomizer = $randomizer;
  21. }
  22. /**
  23. * Determine if the user is authorized to make this request.
  24. *
  25. * @return bool
  26. */
  27. public function authorize(): bool
  28. {
  29. return $this->randomizer->getNumber() > 0.5;
  30. }
  31. /**
  32. * Get the validation rules that apply to the request.
  33. *
  34. * @return null|Constraint|Constraint[]
  35. */
  36. public function rules()
  37. {
  38. return new Assert\Collection([
  39. 'fields' => [
  40. 'email' => [
  41. new Assert\NotBlank(),
  42. new Assert\NotNull(),
  43. new Assert\Email(),
  44. ],
  45. 'firstName' => new Assert\Length(['max' => 255]),
  46. 'lastName' => new Assert\Optional([
  47. new Assert\Length(['max' => 3]),
  48. ]),
  49. ],
  50. ]);
  51. }
  52. }
  1. // src/Controller/ApiTestController.php
  2. namespace App\Controller;
  3. use App\Request\UserRequest;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. class ApiTestController extends AbstractController
  6. {
  7. public function index(UserRequest $form)
  8. {
  9. $email = $form->getRequest()->request->get('email');
  10. }
  11. }

License

It is released under the MIT License.