Symfony 2 - collecting form for one object without parent

I have a problem with a collection of forms. I want to show one form with all values ​​from one object, and I want to be able to add or remove some record (row) from that object on one page. I have the following solutions: it's ok.

CurrencyController

class CurrencyController extends Controller {
/**
 * @Template()
 */
public function testAction() {
    $em = $this->getDoctrine()->getManager();

    $currencies = $em->getRepository('MyWebBundle:Currency')->findAll();

    $arr = array('currencies' => $currencies);
    $form = $this->createFormBuilder($arr)
    ->add('currencies', 'collection', array(
    'type' => new CurrencyType(),
                'allow_add'    => true,
                'allow_delete'    => true,
                'by_reference' => false,
    ))
    ->add('submit', 'button', array('label' => 'Odeslat'))
    ->getForm();

    return array(
        'form' => $form->createView(),
    );
}
}

      

CurrencyType

class CurrencyType extends AbstractType {

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('abbreviation', 'text')
            ->add('rate', 'number')
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'My\WebBundle\Entity\Currency'
    ));
}

/**
 * @return string
 */
public function getName() {
    return 'currency';
}
}

      

Twig

{% extends '::base.html.twig' %}

{% block body -%}
<h1>Test</h1>

{{ form(form) }}

{% endblock %}

      


If I use form class for CurrenciesType then Symfony throws an exception

Notice: Object of class My\WebBundle\Entity\Currency could not be converted to int in             
....\web\vendor\symfony\symfony\src\Symfony\Component\
Form\Extension\Core\ChoiceList\ChoiceList.php line 462

      

The code for this is below.

CurrencyController

class CurrencyController extends Controller {
/**
 * @Template()
 */
public function testAction() {
    $em = $this->getDoctrine()->getManager();

    $currencies = $em->getRepository('MyWebBundle:Currency')->findAll();

    $arr = array('currencies' => $currencies);
    $form = $this->createForm(new CurrenciesType(), $arr);

    return array(
        'form' => $form->createView(),
    );
}
}

      

CurrenciesType

class CurrenciesType extends AbstractType {

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('currencies', 'collection', array(
                'type' => new CurrencyType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
            ))
            ->add('submit', 'button', array('label' => 'Send'))
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => null
    ));
}

/**
 * @return string
 */
public function getName() {
    return 'my_webbundle_currencies';
}
}

      

CurrencyType and Twig are the same as above.

I found solution solution # 1 solution # 2 , but my Symfony still throws the exception as above and I don't see any other in my solution and these solutions. Please help with this problem. Thanks everyone :)

+3


source to share





All Articles