2017 © Pedro Peláez
 

symfony-bundle form-generator-bundle

Symfony Bundle for dynamic Form generation

image

codete/form-generator-bundle

Symfony Bundle for dynamic Form generation

  • Wednesday, December 6, 2017
  • by malarzm
  • Repository
  • 8 Watchers
  • 28 Stars
  • 6,587 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 9 Versions
  • 7 % Grown

The README.md

FormGeneratorBundle

Build Status SensioLabsInsight, (*1)

We were extremely bored with writing/generating/keeping-up-to-date our FormType classes so we wanted to automate the process and limit required changes only to Entity/Document/Whatever class and get new form out of the box - this is how FormGenerator was invented., (*2)

You're looking at the documentation for version 2.0, (*3)

Basic Usages

Consider a class, (*4)

``` php use Codete\FormGeneratorBundle\Annotations as Form; // import Symfony form types so ::class will work, (*5)

/** * @Form\Form( * personal = { "title", "name", "surname", "photo", "active" }, * work = { "salary" }, * admin = { "id" = { "type" = NumberType::class }, "surname" } * ) */ class Person { public $id;, (*6)

/**
 * @Form\Field(type=ChoiceType::class, choices = { "Mr." = "mr", "Ms." = "ms" })
 */
public $title;

/**
 * @Form\Field(type=TextType::class)
 */
public $name;

/**
 * @Form\Field(type=TextType::class)
 */
public $surname;

/**
 * @Form\Field(type=FileType::class)
 */
public $photo;

/**
 * @Form\Field(type=CheckboxType::class)
 */
public $active;

/**
 * @Form\Field(type=MoneyType::class)
 */
public $salary;

}, (*7)


Now instead of writing whole ``PersonFormType`` and populating FormBuilder there we can use instead: ``` php use Codete\FormGeneratorBundle\FormGenerator; $generator = $this->get(FormGenerator::class); $person = new Person(); $form = $generator->createFormBuilder($person)->getForm(); $form->handleRequest($request);

Voila! Form for editing all annotated properties is generated for us. We could even omit type=".." in annotations if Symfony will be able to guess the field's type for us., (*8)

Specifying Field Options

By default everything you specify in @Form\Field (except for type) annotation will be passed as an option to generated form type. To illustrate:, (*9)

/**
 * @Form\Field(type=ChoiceType::class, choices = { "Mr." = "mr", "Ms." = "ms" }, "attr" = { "class" = "foo" })
 */
public $title;

is equivalent to:, (*10)

$fb->add('title', ChoiceType::class, [
    'choices' => [ 'Mr.' => 'mr', 'Ms.' => 'ms' ],
    'attr' => [ 'class' => 'foo' ],
]);

This approach has few advantages like saving you a bunch of keystrokes each time you are specifying options, but there are downsides too. First, if you have any custom option for one of your modifiers you forget to unset, Symfony will be unhappy and will let you know by throwing an exception. Another downside is that we have reserved type property and it's needed as an option for the repeated type. If you ever find yourself in one of described cases, or you just prefer to be explicit, you can put all Symfony fields' options into an options property:, (*11)

/**
 * @Form\Field(
 *   type=ChoiceType::class,
 *   options={ "choices" = { "Mr." = "mr", "Ms." = "ms" }, "attr" = { "class" = "foo" } }
 * )
 */
public $title;

When Form Generator creates a form field and finds options property, it will pass them as that field's options to the FormBuilder. Effectively this allows you to separate field's options from options for your configuration modifiers which can be a gain on its own., (*12)

Adding fields not mapped to a property

Sometimes you may need to add a field that will not be mapped to a property. An example of such use case is adding buttons to the form:, (*13)

/**
 * The first value in Field annotation specifies field's name.
 *
 * @Form\Field("reset", type=ResetType::class)
 * @Form\Field("submit", type=SubmitType::class, "label"="Save")
 */
class Person

All fields added on the class level come last in the generated form, unless a form view (described below) specifies otherwise. Contrary to other class-level settings, @Fields will not be inherited by child classes., (*14)

Form Views

In the example we have defined additional form views in @Form\Form annotation so we can add another argument to createFormBuilder, (*15)

``` php $form = $generator->createFormBuilder($person, 'personal')->getForm();, (*16)


And we will get Form with properties specified in annotation. We can also add/override fields and their properties like this: ``` php /** * @Form\Form( * work = { "salary" = { "attr" = { "class" = "foo" } } } * ) */ class Person

But if you need something more sophisticated than Annotations we have prepared few possibilities that can be either added manually or by tagging your services. For each of them FormGenerator allows you to pass any additional informations you want in optional $context argument. Both ways allows you to specify priority which defines order of execution (default is 0, if two or more services have same priority then first added is executed first)., (*17)

If you have enabled Service autoconfiguration the bundle will automatically tag services for you., (*18)

FormViewProvider

These are used to provide fields list and/or basic configuration for Forms and are doing exactly same thing as @Form\Form annotation., (*19)

Tag for service: form_generator.view_provider, (*20)

FormConfigurationModifier

These can modify any form configuration provided by class itself or FormViewProviders. Feel free to remove or add more stuff to your Form or tweak existing configuration, (*21)

Tag for service: form_generator.configuration_modifier, (*22)

``` php class InactivePersonModifier implements FormConfigurationModifierInterface { public function modify($model, $configuration, $context) { unset($configuration['salary']); return $configuration; }, (*23)

public function supports($model, $configuration, $context) 
{
    return $model instanceof Person && $model->active === false;
}

}, (*24)


FormFieldResolver ----------------- These are responsible for creating actual field in Form and can be used for instance to attach Transformers to your fields. Tag for service: ``form_generator.field_resolver`` ``` php class PersonSalaryResolver implements FormFieldResolverInterface { public function getFormField(FormBuilderInterface $fb, $field, $type, $options, $context) { $transformer = new /* ... */; return $fb->create($field, $type, $options) ->addViewTransformer($transformer); } public function supports($model, $field, $type, $options, $context) { return $model instanceof Person && $field === 'salary'; } }

Embedded Forms

If you need embedded forms we got you covered:, (*25)

``` php /** * @Form\Embed(class="Codete\FormGeneratorBundle\Tests\Model\Person") */ public $person;, (*26)


Such sub-form will contain all annotated properties from given model. To specify a view for the generated embedded form just specify it in the configuration: ``` php /** * @Form\Embed( * class="Codete\FormGeneratorBundle\Tests\Model\Person", * view="work" * ) */ public $employee;

The Versions

06/12 2017

dev-master

9999999-dev http://codete.com

Symfony Bundle for dynamic Form generation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maciej Malarz

form generator dynamic form

04/12 2017

2.0.0

2.0.0.0 http://codete.com

Symfony Bundle for dynamic Form generation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maciej Malarz

form generator dynamic form

30/10 2017

dev-remove-display-annotation

dev-remove-display-annotation http://codete.com

Symfony Bundle for dynamic Form generation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maciej Malarz

form generator dynamic form

14/10 2017

1.3.0

1.3.0.0 http://codete.com

Symfony2 Bundle for dynamic Form generation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maciej Malarz

form generator dynamic form

22/09 2017

dev-additional-fields-v2

dev-additional-fields-v2 http://codete.com

Symfony2 Bundle for dynamic Form generation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maciej Malarz

form generator dynamic form

26/07 2017

dev-additional-fields

dev-additional-fields http://codete.com

Symfony2 Bundle for dynamic Form generation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maciej Malarz

form generator dynamic form

01/06 2017

1.2.0

1.2.0.0 http://codete.com

Symfony2 Bundle for dynamic Form generation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maciej Malarz

form generator dynamic form

04/04 2017

1.1.0

1.1.0.0 http://codete.com

Symfony2 Bundle for dynamic Form generation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maciej Malarz

form generator dynamic form

27/10 2016

1.0.0

1.0.0.0 http://codete.com

Symfony2 Bundle for dynamic Form generation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maciej Malarz

form generator dynamic form