Symfony 3 - static CollectionType children not showing up

Asked
Active3 hr before
Viewed126 times

7 Answers

symfonystatic
90%

Meta Stack Overflow ,So it seems that the children of the collection get cleared somewhere between being added in my custom Type and the form being made available in the Controller.,I am working on a form in Symfony 3.1.3 and want to add a dynamic amount of checkboxes using CollectionType. In the buildForm-method of my Type-class I am using this code to add the Checkboxes:, Stack Overflow Public questions & answers

So in my SpecialType.php I just have:

    $builder - > add('levels', CollectionType::class, array(
       'entry_type' => CheckboxType::class,
       'required' => false,
       'mapped' => false,
    ));
load more v
88%

In both cases, no input fields would render unless your emails data array already contained some emails.,If you are working with a collection of Doctrine entities, pay special attention to the allow_add, allow_delete and by_reference options. You can also see a complete example in the How to Embed a Collection of Forms article.,Following along with the previous example, suppose you start with two emails in the emails data array. In that case, two input fields will be rendered that will look something like this (depending on the name of your form):,If you wish the field to be ignored when reading or writing to the object, you can set the mapped option to false.

1
2
# replace 'FooType'
by the class name of your form type
$ php bin / console debug: form FooType
load more v
72%

@searbe You are misinterpreting the collection type. This type expects a single-level array (collection), not a multi-leveled one, so even if you add your data transformer this can never work.,It doesn't need to be implemented in the collection type, however it is quite unexpected that this can't be solved with a data transformer.,I was expecting model->norm transformation to happen almost instantly, so the whole form system would expect normalised data.,What you want instead is a collection of collection fields (one for the outer array, one for each inner one). Alternatively, if for example the outer keys are fixed, you can add the collection fields statically:

$builder - > add('employees', 'collection', array(
   'type' => 'employee',
   'index_by' => 'sin',
));
load more v
65%

So this should render the same thing as before, but with a bit more styling. Refresh! Ok, it's better... but what's up with those zero, one, two, three labels?, 19. Embedded Form: CollectionType 6:36 ,This genusScientistForm is actually an entire form full of several fields. So, it prints out a label for the entire form... which is zero, one, two, three, and four. That's not helpful!, 25. Customizing the Collection Form Prototype 5:56

What PHP libraries does this tutorial use?

// composer.json
{
   "require": {
      "php": "^7.1.3",
      "symfony/symfony": "3.4.*", // v3.4.49
      "doctrine/orm": "^2.5", // 2.7.5
      "doctrine/doctrine-bundle": "^1.6", // 1.12.13
      "doctrine/doctrine-cache-bundle": "^1.2", // 1.4.0
      "symfony/swiftmailer-bundle": "^2.3", // v2.6.7
      "symfony/monolog-bundle": "^2.8", // v2.12.1
      "symfony/polyfill-apcu": "^1.0", // v1.23.0
      "sensio/distribution-bundle": "^5.0", // v5.0.25
      "sensio/framework-extra-bundle": "^3.0.2", // v3.0.29
      "incenteev/composer-parameter-handler": "^2.0", // v2.1.4
      "composer/package-versions-deprecated": "^1.11", // 1.11.99.4
      "knplabs/knp-markdown-bundle": "^1.4", // 1.9.0
      "doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
      "stof/doctrine-extensions-bundle": "^1.2" // v1.3.0
   },
   "require-dev": {
      "sensio/generator-bundle": "^3.0", // v3.1.7
      "symfony/phpunit-bridge": "^3.0", // v3.4.47
      "nelmio/alice": "^2.1", // v2.3.6
      "doctrine/doctrine-fixtures-bundle": "^2.3" // v2.4.1
   }
}
75%

{# templates/base.html.twig #}

{# ... #}
<div id="sidebar">
    {# if the controller is associated with a route, use the path() or url() functions #}
    {{ render(path('latest_articles', {max: 3})) }}
    {{ render(url('latest_articles', {max: 3})) }}

    {# if you don't want to expose the controller with a public URL,
       use the controller() function to define the controller to execute #}
    {{ render(controller(
        'App\\Controller\\BlogController::recentArticles', {max: 3}
    )) }}
</div>
40%

Whether or not the parent of a service is abstract, these are the properties a child service definition inherits from its parent definition:,The setup is open for extension, since you can add the name of another storage manager to the list in the Configuration class and then add a service definition file with the necessary services and an alias.,Make sure that the service you create for this event listener will receive the annotation_reader service as its first constructor argument.,When the resources are defined like this, you can create service definitions for them in the container extension:

The environment can be any string. It is mainly a way to determine which configuration file should be loaded (e.g. config_dev.yml or config_prod.yml). This is made explicit in AppKernel:

public
function registerContainerConfiguration(LoaderInterface $loader) {
   $loader
      -
      > load(__DIR__.
         '/config/config_'.$this - > getEnvironment().
         '.yml');
}
load more v
22%

0

Other "symfony-static" queries related to "Symfony 3 - static CollectionType children not showing up"