|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bdf\Form\Bundle\Http\Submit; |
| 4 | + |
| 5 | +use Bdf\Form\Aggregate\FormInterface; |
| 6 | +use Bdf\Form\Bundle\Http\PayloadSource; |
| 7 | +use Bdf\Form\Custom\CustomForm; |
| 8 | +use Symfony\Component\HttpKernel\Attribute\ValueResolver; |
| 9 | +use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
| 10 | + |
| 11 | +/** |
| 12 | + * Mark the controller parameter as a form to be submitted. |
| 13 | + * |
| 14 | + * Usage: |
| 15 | + * ```php |
| 16 | + * class MyController |
| 17 | + * { |
| 18 | + * public function simpleForm(#[SubmitForm] MyForm $form): Response |
| 19 | + * { |
| 20 | + * // Form is submitted using values depending on the HTTP method, and validated |
| 21 | + * // The form class is resolved from the parameter type. You can manually set it using the `form` parameter. |
| 22 | + * // If the form is invalid, an InvalidFormException will be thrown |
| 23 | + * |
| 24 | + * assert($form->valid()); // Always true |
| 25 | + * // ... |
| 26 | + * } |
| 27 | + * |
| 28 | + * public function manualValidation(#[SubmitForm(validate: false)] MyForm $form): Response |
| 29 | + * { |
| 30 | + * // Work like the previous example, but the form is not validated |
| 31 | + * // So you have to call $form->validate() manually |
| 32 | + * |
| 33 | + * if (!$form->valid()) { |
| 34 | + * // Handle the error |
| 35 | + * } |
| 36 | + * |
| 37 | + * // ... |
| 38 | + * } |
| 39 | + * |
| 40 | + * public function useValue(#[SubmitForm(form: MyForm::class)] MyValue $value): Response |
| 41 | + * { |
| 42 | + * // The form is submitted, validated, and it's value is generated using the FormInterface::value() method |
| 43 | + * } |
| 44 | + * |
| 45 | + * public function withCustomSources(#[SubmitForm(source: [PayloadSource::Attributes, PayloadSource::Body])] MyForm $form): Response |
| 46 | + * { |
| 47 | + * // You can define the source of the payload using the `source` parameter, instead of relying on the HTTP method |
| 48 | + * // Multiple sources can be defined, and all values will be merged. The first source takes the priority over following ones, |
| 49 | + * // So field that are defined in multiple sources will not be overridden. |
| 50 | + * } |
| 51 | + * } |
| 52 | + * ``` |
| 53 | + */ |
| 54 | +#[\Attribute(\Attribute::TARGET_PARAMETER)] |
| 55 | +final class SubmitForm extends ValueResolver |
| 56 | +{ |
| 57 | + public ArgumentMetadata $metadata; |
| 58 | + |
| 59 | + public function __construct( |
| 60 | + /** |
| 61 | + * The request payload source. |
| 62 | + * |
| 63 | + * By default, it will be determined based on the HTTP method. |
| 64 | + * If an array is given, all sources will be merged, with the first one taking precedence. |
| 65 | + * |
| 66 | + * @var PayloadSource|PayloadSource[] |
| 67 | + */ |
| 68 | + public PayloadSource|array $source = PayloadSource::Auto, |
| 69 | + |
| 70 | + /** |
| 71 | + * The form class to use. |
| 72 | + * If null, it will be determined based on the argument type. |
| 73 | + * |
| 74 | + * @var class-string<CustomForm>|null |
| 75 | + */ |
| 76 | + public ?string $form = null, |
| 77 | + |
| 78 | + /** |
| 79 | + * If true, the form will be validated, and {@see InvalidFormException} will be thrown if the form is invalid. |
| 80 | + */ |
| 81 | + public bool $validate = true, |
| 82 | + |
| 83 | + /** |
| 84 | + * The error message to return if the form is invalid. |
| 85 | + */ |
| 86 | + public string $validateMessage = 'The JSON contains invalid data.', |
| 87 | + |
| 88 | + /** |
| 89 | + * Get the value instead of the form instance. |
| 90 | + * |
| 91 | + * If null, this flag will be resolved based on the parameter type (i.e. if the controller parameter type is `FormInterface`, it will be set to false). |
| 92 | + * |
| 93 | + * Note: if this flag is set to true, the form class must be defined on {@see SubmitForm::$form} parameter. |
| 94 | + * |
| 95 | + * @see FormInterface::value() will be called to get the value. |
| 96 | + */ |
| 97 | + public ?bool $value = null, |
| 98 | + ) { |
| 99 | + parent::__construct(SubmitFormValueResolver::class); |
| 100 | + } |
| 101 | +} |
0 commit comments