Symfony form creates new object and create first o

2019-08-10 21:37发布

I have an entity for support tickets: SupportTicket(). I also have an entry for replies to each ticket: SupportEntry(). I setup a one-to-many relationship between SupportTicket() and SupportEntry().

Now what I'm trying to do is build my form so that it creates the initial SupportTicket and then inserts the first SupportEntry, all in the same form. I've been messing around with my code for a while, only half-understanding what I'm doing, but this is where I'm at right now:

// My controller, creating the form
$supportTicket = new SupportTicket();

    $form = $this->createFormBuilder($supportTicket)
        ->add('subject', 'text', array(
            'label'         => 'Subject'
        ))
        ->add('jobNumber', 'text', array(
            'label'         => 'Job Number'
        ))
        ->add('supportGroup', 'entity', array(
            'label'         => 'Group',
            'class'         => 'ShawmutClientBundle:SupportGroup',
            'property' =>   'name',
            'multiple'      => true,
            'expanded'      => true
        ))
       // ->add('supportEntries', new SupportEntryType())
        ->add('supportEntries', new SupportEntryType())

        ->add('Save', 'submit')

        ->getForm();

My attempt at the custom form type

<?php
namespace Shawmut\ClientBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class SupportEntryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder->add('comment', 'textarea');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Shawmut\ClientBundle\Entity\SupportEntry',
    ));
}

public function getName()
{
    return 'SupportEntryType';
}
}

The form does have the comment box that I've pulled in from the form type, but when I try to submit the form, I get this error:

Neither the property "supportEntries" nor one of the methods "setSupportEntries()", "_set()" or "_call()" exist and have public access in class "Me\MyBundle\Entity\SupportTicket".

And yeah, that makes sense. It should be the addSupportEntries() method which is there. So how do I tell the form builder to use addSupportEntries instead of setSupportEntries?

Thanks in advance

2条回答
别忘想泡老子
2楼-- · 2019-08-10 22:06

Give the collection form type a go.

->add(
    'supportEntries',
    'collection',
    array(
        'type' => new SupportEntryType(),
        'label' => 'Support Entries',
        'error_bubbling' => true,
        'cascade_validation' => true,
    )
)
查看更多
老娘就宠你
3楼-- · 2019-08-10 22:14

If you are using the collection form type, and the textarea is not showing, add:

'allow_add'    => true

to the properties array(). The code would look something like this:

->add(
'supportEntries',
'collection',
array(
    'type' => new SupportEntryType(),
    'label' => 'Support Entries',
    'error_bubbling' => true,
    'allow_add'    => true
    'cascade_validation' => true,
)

)

To show the widget, assuming you are using twig:

  {{ form_widget(form.supportEntries.vars.prototype.comment) }}

For saving the support entry, depending on how you built your entities, you might need to make some extra modifications.

The documentation should help you get it right: How to Embed a Collection of Forms

查看更多
登录 后发表回答