Giter Site home page Giter Site logo

form-generator-bundle's Introduction

micayael/form-generator-bundle

Symfony 5 Symfony 6 Scrutinizer Quality Score Packagist License Latest Stable Version Total Downloads PHP from Packagist

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

  1. el name para el input de formulario
  2. el tipo de campo de formulario
  3. 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:

  1. FormGenerator::createForm(array $formConfig, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): Creates a form from an associative array
  2. FormGenerator::createFormFromJson(string $formConfigJson, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): Creates a form from a json string
  3. FormGenerator::createFormFromYaml(string $formConfigYaml, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): Creates a form from a yaml string

Method arguments

  1. array $formConfig: form configuration
  2. array $formOptions = []: custom form options
  3. $data = null: form data
  4. 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.
  5. 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

vendor/bin/phpstan analyse src tests --level 5
vendor/bin/phpmd ./ text .phpmd-ruleset.xml --exclude var,vendor

form-generator-bundle's People

Contributors

micayael avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.