Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested collection fields #3583

Closed
nielvrom opened this issue Feb 11, 2016 · 4 comments
Closed

Nested collection fields #3583

nielvrom opened this issue Feb 11, 2016 · 4 comments

Comments

@nielvrom
Copy link

I'm having problems creating my form for creating a course. This is a part of my database scheme for which I'm trying to create a form:

schermafbeelding 2016-02-11 om 09 20 31

So which I'm trying to do is create a course where I can create sessions and dates attached to that session. It should look something like this:

schermafbeelding 2016-02-06 om 17 13 53

In my CourseAdmin class I have:

$formMapper
        ->add('name',                   'text',         array('label' => 'Naam'))
        ->add('description',            'textarea',     array('label' => 'Beschrijving'))
        ->add('materials',              'textarea',     array('label' => 'Benodigde materialen'))
        ->add('numberOfParticipants',   'number',       array('label' => 'Aantal deelnembers'))
        ->add('numberOfDays',           'number',       array('label' => 'Aantal dagen'))
        ->add('price',                  'number',       array('label' => 'Prijs'))
        ->add('priceKmo',               'number',       array('label' => 'KMO-portefeuille Prijs'))

        ->add('location', 'sonata_type_model', array('expanded' => true, 'by_reference' => false, 'multiple' => true, 'btn_add' => false))

        ->add('session', 'sonata_type_collection', array(
            'by_reference' => false,
            'type_options' => array(
                // Prevents the "Delete" option from being displayed
                'delete' => false,
                'delete_options' => array(
                    // You may otherwise choose to put the field but hide it
                    'type'         => 'hidden',
                    // In that case, you need to fill in the options as well
                    'type_options' => array(
                        'mapped'   => false,
                        'required' => false,
                    )
                )
            )
        ), array(
            'edit' => 'inline',
            'inline' => 'table',
            'sortable' => 'position'
        ))
    ;

In my SessionAdmin class I have:

$formMapper
        ->add('type',      'text',     array('label' => 'Type opleiding (Dag / Avond)'))
        ->add('moment', 'sonata_type_collection', array(
            'by_reference' => false,
            'type_options' => array(
                // Prevents the "Delete" option from being displayed
                'delete' => false,
                'delete_options' => array(
                    // You may otherwise choose to put the field but hide it
                    'type'         => 'hidden',
                    // In that case, you need to fill in the options as well
                    'type_options' => array(
                        'mapped'   => false,
                        'required' => false,
                    )
                )
            )
        ), array(
            'edit' => 'inline',
            'inline' => 'table',
            'sortable' => 'position'
        ))
    ;

And in my MomentAdmin class I have:

$formMapper
        ->add('time',      'date',     array('label' => 'Datum'))
    ;

The problem in my form is when I try to add a date to my session I get the following error:

FatalErrorException: Error: Call to a member function getName() on null in /myproject/app/cache/dev/classes.php line 9772

So, I can add a session but when I try to add a date to my session I'm getting the error ... .
The function causing the error is the following:

public function appendFormFieldElement(AdminInterface $admin, $subject, $elementId)
{
    $formBuilder = $admin->getFormBuilder();
    $form = $formBuilder->getForm();
    $form->setData($subject);
    $form->submit($admin->getRequest());
    $childFormBuilder = $this->getChildFormBuilder($formBuilder, $elementId);
    $fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName());

    try {
        $value = $fieldDescription->getValue($form->getData());
    } 
    catch (NoValueException $e) {
        $value = null;
    }

    $data = $admin->getRequest()->get($formBuilder->getName());

    if (!isset($data[$childFormBuilder->getName()])) {
        $data[$childFormBuilder->getName()] = array();
    }

    $objectCount = count($value);
    $postCount = count($data[$childFormBuilder->getName()]);
    $fields = array_keys($fieldDescription->getAssociationAdmin()->getFormFieldDescriptions());
    $value = array();

    foreach ($fields as $name) {
        $value[$name] ='';
    }

    while ($objectCount < $postCount) {
        $this->addNewInstance($form->getData(), $fieldDescription);
        ++$objectCount;
    }

    $this->addNewInstance($form->getData(), $fieldDescription);
    $data[$childFormBuilder->getName()][] = $value;
    $finalForm = $admin->getFormBuilder()->getForm();
    $finalForm->setData($subject);
    $finalForm->setData($form->getData());

    return array($fieldDescription, $finalForm);
}

And the rule that's causing the error is :

$fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName());

@efiorello
Copy link

I think your problem is caused by not having created the method public function __toString() in your entity.

@nielvrom
Copy link
Author

All my entities have a __toString() function ... .

@chalasr
Copy link

chalasr commented Feb 27, 2016

Possible duplicate of #262, #1228, #1327 and #1971 .
Fixed by PR #3553 .
I answered you on SO.

@jordisala1991
Copy link
Member

Closing, as @chalasr said (thanks btw), it was fixed on #3553

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants