Symfony form for a general entity with a type and different options

Tofandel

I'm new to symfony and I'm experiencing a bit with it's functionalities. I'm facing a problem and I will simplify it for better understanding : let's say we have a Shape Doctrine Entity (I want only one table to store different kind of shapes) :

class Shape {
   protected $id;
   protected $type;
   protected $options;
}

Depending of the shape type, the options will differ :

class Rectangle extends Shape {
    protected $options = array('width' => 20, 'height' => 20);
    protected $type = 'rectangle';
}
class Circle extends Shape {
    protected $options = array('radius' => 15);
    protected $type = 'circle';
}

Now I would like to create a generalist form with the formBuilder for adding/creating such entities (I'm using sonata but it's not very important)

So with a Choice input for the type and other inputs for the options that will change depending of the type choosen. (I have a function that returns an array of the available options and their type on each extended class)

.content {
  font-family: Arial;
}
<form class="content">
  <label>Type : </label><select name="type">
    <option value="circle">Circle</option>
    <option value="rectangle">Rectangle</option>
  </select>

  <fieldset>
    <legend>Circle</legend>
    <input type="number" name="radius" placeholder="Radius">
  </fieldset>
  <fieldset>
    <legend>Rectangle</legend>
    <input type="number" name="width" placeholder="Width">
    <input type="number" name="height" placeholder="Height">
  </fieldset>
</form>

Is this approach correct ?

And how could I implement this form ? (My first thoughts would be some ajax or directly outputing every inputs for every options and then a javascript function that will display the right ones depending of the choosen type)

Any opinion/better approach is greatly appreciated.

lordrhodos

I usually do the following steps:

1. Doctrine Inheritance Mapping

First make sure you use one of the doctrine inheritance mapping approaches for your entities, e.g. the 'JOINED' table method:

# AppBundle/Entity/Shape.php
use Doctrine\ORM\Mapping as ORM;

/**  
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorMap({
 *     "rectangle" : "AppBundle\Entity\Rectangle",
 *     "circle" : "AppBundle\Entity\Circle"
 * })
 */
class Shape {
   protected $id;
   protected $type;
   protected $options;
}

2. Set the child classes in your admin service definition:

# app/config/services.yml
app.admin.shape:
    class: AppBundle\Admin\ShapeAdmin
    arguments: [~, AppBundle\Entity\Shape, SonataAdminBundle:CRUD]
    tags:
        - { name: sonata.admin, manager_type: orm, group: admin, label: InterestService }
    calls:
        - [setSubclasses, [{'Rectangle': AppBundle\Entity\Rectangle, 'Circle': AppBundle\Entity\Circle}]]

3. check type of object in admin class

Now you can check inside the admin class for the type of the subject, and manipulate the views to your liking. You could, e.g. change the edit form in the configureFormFields method:

# AppBundle/Admin/ShapeAdmin.php
protected function configureFormFields(FormMapper $formMapper)

    /** @var Shape $shape */
    $shape = $this->getSubject();

    // add form fields for both types
    $formMapper
        ->add('service', null, ['disabled' => true]);

    // add specific form fields
    if ($shape instanceof Rectangle) {

       // custom rectangle form fields

    } elseif ($shape instanceof Circle {

       // custom circle form fields

    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony 2 Transformer on entity form type

From Dev

symfony entity form type with related data

From Dev

Symfony creating choice from entity in form type

From Dev

Symfony 2.8 form entity type custom property

From Dev

Symfony form type entity data transformerm

From Dev

Symfony 2.7 Form Entity type render multiple properties in form

From Dev

Symfony 2.7 Form Entity type render multiple properties in form

From Dev

field array type in entity for form choice type field symfony

From Dev

symfony2 form create new type combining collection and entity

From Dev

Additional properties to entity Field Type in a form in Symfony2

From Dev

Symfony2 entity form type not saving many to many

From Dev

Symfony2 - How to validate autocomplete entity form type?

From Dev

Symfony2 form type entity add extra option

From Dev

Adding a default value along with entity in form type of Symfony2

From Dev

Symfony3 form type, reference other entity by id

From Dev

Symfony update entity with form

From Dev

Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

From Dev

Add custom options to symfony form

From Dev

Symfony Choice type with disabled options

From Dev

Symfony Choice type with disabled options

From Dev

Nullable custom form entity with Symfony

From Dev

Symfony Form Collection with Singular Entity

From Dev

Symfony validate entity using form

From Dev

Symfony2 generate entity fields with options

From Dev

Symfony 2.3 pass custom data to entity form, using choice or other type

From Dev

symfony2 - How to create a form field of type "entity" without values

From Dev

Symfony3.0 When saving a form with Entity Type dropdown; SQL error cannot insert null

From Dev

Symfony3.0 When saving a form with Entity Type dropdown; SQL error cannot insert null

From Dev

Symfony "spl_object_hash() expects parameter 1 to be object, string given" when editing with form entity type

Related Related

  1. 1

    Symfony 2 Transformer on entity form type

  2. 2

    symfony entity form type with related data

  3. 3

    Symfony creating choice from entity in form type

  4. 4

    Symfony 2.8 form entity type custom property

  5. 5

    Symfony form type entity data transformerm

  6. 6

    Symfony 2.7 Form Entity type render multiple properties in form

  7. 7

    Symfony 2.7 Form Entity type render multiple properties in form

  8. 8

    field array type in entity for form choice type field symfony

  9. 9

    symfony2 form create new type combining collection and entity

  10. 10

    Additional properties to entity Field Type in a form in Symfony2

  11. 11

    Symfony2 entity form type not saving many to many

  12. 12

    Symfony2 - How to validate autocomplete entity form type?

  13. 13

    Symfony2 form type entity add extra option

  14. 14

    Adding a default value along with entity in form type of Symfony2

  15. 15

    Symfony3 form type, reference other entity by id

  16. 16

    Symfony update entity with form

  17. 17

    Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

  18. 18

    Add custom options to symfony form

  19. 19

    Symfony Choice type with disabled options

  20. 20

    Symfony Choice type with disabled options

  21. 21

    Nullable custom form entity with Symfony

  22. 22

    Symfony Form Collection with Singular Entity

  23. 23

    Symfony validate entity using form

  24. 24

    Symfony2 generate entity fields with options

  25. 25

    Symfony 2.3 pass custom data to entity form, using choice or other type

  26. 26

    symfony2 - How to create a form field of type "entity" without values

  27. 27

    Symfony3.0 When saving a form with Entity Type dropdown; SQL error cannot insert null

  28. 28

    Symfony3.0 When saving a form with Entity Type dropdown; SQL error cannot insert null

  29. 29

    Symfony "spl_object_hash() expects parameter 1 to be object, string given" when editing with form entity type

HotTag

Archive