简体   繁体   中英

Nested form: passing values

I'm a bit at a loss here with Symfony nested forms..

I have Events and RoleEntries ie the role that people or organizations can have for an event...

So I have an EventType form, with a nested RoleEntryType:

->addEventListener(FormEvents::PRE_SET_DATA,
                function (FormEvent $event) {
                    $form = $event->getForm();
                    $data = $event->getData();


                    $form->add('roleEntries', 'collection', array(
                        'type'         => new RoleEntryType($data),
                        'allow_add'    => true,
                        'allow_delete' => true
                        ));
                }
            )

the $data variable is caught by the RoleEntryType constructor:

$this->data=$data;

And I try to add a hidden field (since the user should not modify it, in the field):

 ->add($this->targetScope, 'hidden',  array('data'=>$this->data))

At this point, Symfony is not happy because it cannot convert the $data to string

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class IH\\EventManagerBundle\\Entity\\Event could not be converted to string") in form_div_layout.html.twig at line 13.

so I try just to give it an Id

 ->add($this->targetScope, 'hidden',  array('data'=>$this->data->getId()))

But it doesn't work either because a string is not enough, it wants a fully fledged event:

Catchable Fatal Error: Argument 1 passed to IH\\EventManagerBundle\\Entity\\RoleEntry::setEvent() must be an instance of IH\\EventManagerBundle\\Entity\\Event, string given, called in /Users/MTP/Documents/dev/MTP/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 410 and defined

So I guess I'm doing it all wrong....

help!

$ data包含表单数据,因此您需要提取字段值并将其作为隐藏字段的值传递:

$form->add('targetScope', 'hidden',  array('data' => $data['field_name']));

Without change your initial implantation, just add a __to string() magic method to Event entity and all should work as expected

Something like

Class Event
{
    [...]
    public function __toString() 
    {
        return $this->name;
    }
}

Of course $this->name; should be changed accordingly to your object properties.

If you need, when posting the form, to recreate (or take from db) an entity object, take a look to DataTransformers

Thanks for all your replies,

So using DataTransformer or creating a new field type (EntityHidden) both work... however, my problem stemed from something I forgot in my code which forced me to handle this in the FormType rather than in the entity...

Simply, there is no need to pass the primary key of the entity corresponding to the nesting form (Event) over to the nested form (RoleEntryType)... if I tell my Event entity setter that it should write a reference of the event in the new RoleEntry:

public function addRoleEntry(
{
    $this->roleEntries[] = $roleEntry;

    $roleEntry->setEvent($this);

    return $this;
}

Sorry if my question wasn't clear and thanks for taking the time to answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM