micayael / form-generator-bundle
Bundle para el generar formularios mediante yaml y json
Installs: 4 241
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.4|^8.0
- ext-json: *
- symfony/config: ^5.1|^6.0
- symfony/dependency-injection: ^5.1|^6.0
- symfony/form: ^5.1|^6.0
- symfony/http-kernel: ^5.1|^6.0
- symfony/validator: ^5.1|^6.0
- symfony/yaml: ^5.1|^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0|^3.1
- phpmd/phpmd: @stable
- phpstan/phpstan: ^1.4
- symfony/framework-bundle: ^5.1|^6.0
- symfony/test-pack: ^1.0
- symfony/var-dumper: ^5.1|^6.0
README
Introduction
Allows you to generate Symfony forms using YAML, JSON or associative array configurations based on the standard configurations of the framework's forms component.
See: https://symfony.com/doc/current/reference/forms/types.html
It is based on the use of the add function of the form builder used to create forms with Symfony
- el name para el input de formulario
- el tipo de campo de formulario
- un array de options para configurar este tipo de campo
// FormInterface::add($child, string $type = null, array $options = []) $builder->add($inputName, $inputTypeClass, $inputOptions);
With this it is possible to create a YAML configuration, JSON or an associative array in which:
- the key represents the $inputName
- type represents a form $inputTypeClass supported by the
- bundle or the "fully qualified class name (FQN)" of a class form type
- the options array represents the form $inputOptions
This allows you to configure forms dynamically by using yaml, json or an associative array like the examples below:
YAML
name: birthday: type: date options: label: 'Your Birthday' status: type: choice options: choices: Active: A Inactive: I custom_type: type: App\Form\Type\CustomType options: custom_option: value
JSON
{ "name": null, "birthday": { "type": "date", "options": { "label": "Your Birthday" } }, "status": { "type": "choice", "options": { "choices": { "Active": "A", "Inactive": "I" } } }, "custom_type": { "type": "App\\Form\\Type\\CustomType", "options": { "custom_option": "value" } } }
PHP
$formConfig = [ 'name' => NULL, 'birthday' => [ 'type' => 'date', 'options' => [ 'label' => 'Your Birthday', ], ], 'status' => [ 'type' => 'choice', 'options' => [ 'choices' => [ 'Active' => 'A', 'Inactive' => 'I', ], ], ], 'custom_type' => [ 'type' => 'App\\Form\\Type\\CustomType', 'options' => [ 'custom_option' => 'value', ], ], ]
Installation
composer require micayael/form-generator-bundle
Usage
Where it is necessary to create a form, for example a controller or a service, you must inject the FormGenerator object provided by the bundle.
class HomeController extends AbstractController { /** @required */ public FormGenerator $formGenerator; public function __invoke(Request $request): Response { $formConfigArray = []; // configuration as associative array // Gets a FormInterface object with the configured form $form = $this->formGenerator->createForm($formConfigArray); $form->handleRequest($request); if($form->isSubmitted() && $form->isValid()){ $formData = $form->getData(); // process your form } }
The FormGenerator service provides the following methods:
- FormGenerator::createForm(array $formConfig, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): Creates a form from an associative array
- FormGenerator::createFormFromJson(string $formConfigJson, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): Creates a form from a json string
- FormGenerator::createFormFromYaml(string $formConfigYaml, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): Creates a form from a yaml string
Method arguments
- array $formConfig: form configuration
- array $formOptions = []: custom form options
- $data = null: form data
- string $baseFormTypeClass = null: Form class (FQN) that will be used as the base to add the other fields. If not passed, they are added to an empty form.
- string $groupName = null: name that will be used to group the fields created by the $formConfig argument as an embeded form
Development
Install dependencies
composer install
Testing
vendor/bin/phpunit
Code Review
- phpstan levels: https://phpstan.org/user-guide/rule-levels
vendor/bin/phpstan analyse src tests --level 5
vendor/bin/phpmd ./ text .phpmd-ruleset.xml --exclude var,vendor