victor-codigo / symfony-form-extended
Classes to extend symfony form functionality
Installs: 57
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/victor-codigo/symfony-form-extended
Requires
- doctrine/collections: ~2.2
- symfony/form: ^6.4
- symfony/http-foundation: ^6.4
- symfony/security-csrf: ^6.4
- symfony/translation: ^6.4
- symfony/validator: ^6.4
- victor-codigo/upload-file: ~1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.68
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-symfony: ^2.0
- phpunit/phpunit: ^11.5
README
Classes to extend Symfony form functionality.
Adds:
- Form messages translation
- Add flash bag messages
- Uploaded files handler
Prerequisites
- PHP 8.1
- Symfony 6.4
- Doctrine\Common\Collections\Collection
Stack
Usage
-
Install
composer require victor-codigo/symfony-form-extended -
Classes
- FormFactoryExtended: Its a wrapper for class FormFactoryInterface. Allows to build class FormExtended.
- FormFactoryExtendedInterface: It is an interface for class FormFactoryExtended.
- FormExtended: It is a wrapper that extends Form Symfony functionality.
- FormExtendedInterface: It is an interface for FormExtended class.
- FormTypeBase: Extends Symfony AbstractType class functionality.
- FormTypeExtendedInterface: An interface for FormTypeBase
FormFactoryExtended methods:
Adds following methods to interface Symfony\Component\Form\FormFactoryInterface.
| Method | Description | Params | Return |
|---|---|---|---|
| __construct | creates the builder | 1. Symfony\Component\Form\FormFactoryInterface: Symfony form to use 2. Symfony\Contracts\Translation\TranslatorInterface: Symfony translation bundle 3. VictorCodigo\UploadFile\Adapter\UploadFileService: Upload File bundle 4. Symfony\Component\HttpFoundation\RequestStack: Request |
VictorCodigo\SymfonyFormExtended\Factory |
| createNamedExtended | creates a VictorCodigo\SymfonyFormExtended\FormFormExtended | 1. string: Form name 2. string: Full qualified name form type class 3. string: Translation locale 4. mixed: Form initial data 5. array<string, mixed>: options |
VictorCodigo\SymfonyFormExtended\Form\FormExtendedInterface |
FormExtended methods:
Adds following methods to interface Symfony\Component\Form\FormInterface.
| Method | Description | Params | Return |
|---|---|---|---|
| __construct | Creates the form | 1. Symfony\Component\Form\FormInterface: Symfony form to use 2. VictorCodigo\SymfonyFormExtended\Form\FormExtendedConstraints: Class to manage form constraints 3. VictorCodigo\SymfonyFormExtended\Form\FormExtendedFields: Class to manage form fields 4. VictorCodigo\SymfonyFormExtended\Form\FormExtendedCsrfToken: Class to manage form CSRF token 5. VictorCodigo\SymfonyFormExtended\Form\FormExtendedUpload: Class to manage form file uploads 6. VictorCodigo\SymfonyFormExtended\Form\FormExtendedMessages: Class to manage form messages 7. string: locale code |
VictorCodigo\SymfonyFormExtended\Form\FormExtended |
| getMessageErrorsTranslated | Gets form message errors translated | 1. bool: Whether to include errors of child forms as well 2. bool: Whether to flatten the list of errors in case $deep is set to true |
Doctrine\Common\Collections\Collection<int, FormMessage> |
| getMessagesSuccessTranslated | Gets form messages, when form validation is successful | Doctrine\Common\Collections\Collection<int, FormMessage> | |
| getFlashMessagesData | Gets a type of form messages and clears flash from the stack | 1. string: Messages type | Doctrine\Common\Collections\Collection<int, FormMessage> |
| getFlashMessages | Gets a type of form messages and clears flash from the stack | 1. string: Messages type | Doctrine\Common\Collections\Collection<int, string> |
| addFlashMessagesTranslated | Adds flash messages to Symfony session flash bag | 1. string: Key for success messages 2. string: Key for error messages 3. bool: Whether to include errors of child forms as well |
void |
| getConstraints | Gets form constraints as object | object | |
| getCsrfToken | Gets form CSRF token | string | |
| getFormFields | Gets form fields with the form name. Ej: form_name[field_name]. If field ends in "[]", is considered as field array | 1. array<int, \BackedEnum>: Enum that contains form fields names | object |
| getFormFieldsValues | Gets form fields with the field value. | 1. array<int, \BackedEnum>: Enum that contains form fields names 2. object: Form data validation class |
object |
| getFormTemplateData | Gets general info of the form, ready to use in a template | 1. array<int, \BackedEnum>: Enum that contains form fields names | VictorCodigo\SymfonyFormExtended\Form\FormTemplateData |
| uploadFiles | Sets up form configuration for files uploaded, and move files to a specific path | 1. Symfony\Component\HttpFoundation\Request: Symfony request 2. string: Upload path where files are moved and saved 3. array<int, string>: File names to be removed in the path in the upload path |
VictorCodigo\SymfonyFormExtended\Form\FormExtended |
Example
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use VictorCodigo\SymfonyFormExtended\Factory\FormFactoryExtendedInterface; class Controller extends AbstractController { public function __construct( private FormFactoryExtendedInterface $formFactoryExtended, ) { } public function __invoke(Request $request): Response { $form = $this->formFactoryExtended->createNamedExtended('form_name', FormType::class, 'en'); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $form->uploadFiles($request, '/path/to/upload/files'); } $errorsTranslated = $form->getMessageErrorsTranslated(true); $messageSuccess = $form->getMessagesSuccessTranslated(); $form->addFlashMessagesTranslated('messages_success', 'messages_error', true); } }