Symfony2表单实体类参数

时间:2014-05-26 15:01:47

标签: forms symfony parameters controller entity

我需要在表单实体中使用id值,而我无法找到从控制器获取它的方法。  我尝试了很多选项但没有成功也到处搜索。  这是控制器代码:

    public function addClientAction(Request $request, $idSocietate)
 {

    $societate = $this->getSocietate($idSocietate);
    //$locatii = $this->getLocatii();

    $client = new Clienti();
    $dateClienti = new DateClienti();
    $client->getDateClienti()->add($dateClienti);

    $form = $this->createForm(new FormClientiType($idSocietate), $client);
    $request = $this->get('request');



    if ($request->getMethod() == 'POST') {
        $form->bind($request);

        if ($form->isValid()) {
            // Persist objects to database
            $usr = $this->get('security.context')->getToken()->getUser();

            $em = $this->getDoctrine()
                    ->getManager();
            $client->setAdaugatDe($usr->getUsername());
            $client->setSocietati($societate);
            $em->persist($client);
            $em->flush();
            $this->get('session')->getFlashBag()->set('form-notice', 'Client adaugata cu succes.');

            // Redirect - This is important to prevent users re-posting
            // the form if they refresh the page
            return $this->redirect($this->generateUrl('clienti_adauga',        array('idSocietate' => $idSocietate)));
        }
    }

这是表单实体类:

<?php
 // src/Blogger/BlogBundle/Form/EnquiryType.php

 namespace Mnv\CoreBundle\Form;

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


class FormClientiType extends AbstractType
{
private $idSocietate;

public function __construct($idSocietate) {
    $this->idSocietate = $idSocietate;
    var_dump($idSocietate); exit;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Mnv\CoreBundle\Entity\Clienti',
        'cascade_validation' => true,
        'idSocietate' => $this->idSocietate,
    ));
}

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder->add('cod','text', array('required' => false));
    $builder->add('denumire','text', array('required' => true));
    $builder->add('cod_fiscal','text', array('required' => true));
    $builder->add('cont_analitic','text', array('required' => false));
    $builder->add('telefon','text', array('required' => false));
    $builder->add('email','email', array('required' => false));
    $builder->add('judet','text', array('required' => false));
    $builder->add('adresa','text', array('required' => false));
    $builder->add('nr_reg_comertului','text', array('required' => false));
    $builder->add('cont','text', array('required' => false));
    $builder->add('banca','text', array('required' => false));
    $builder->add('delegat','text',array('required' => false));
    $builder->add('delegat_serie_ci','text',array('required' => false));
    $builder->add('delegat_nr_ci','text',array('required' => false));
    $builder->add('delegat_ci_eliberat','text',array('required' => false));
    $builder->add('delegat_mij_transport','text',array('required' => false));
    $builder->add('agent','text',array('required' => false));
    $builder->add('discount','text',array('required' => false));

    $builder->add('date_clienti', 'collection', array(
        'type' => new FormDateClientiType($options['idSocietate']),
        'cascade_validation' => true,
        'prototype' => true,
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false,
        'required' => false,
        ));

    $builder->add('adauga','submit');



}




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

}

我收到此错误:

ContextErrorException: Warning: Missing argument 1 for       Mnv\CoreBundle\Form\FormClientiType::__construct(), called in C:\wamp\www\mnvproject\src\Mnv\CoreBundle\Controller\FormController.php on line 156 and defined in C:\wamp\www\mnvproject\src\Mnv\CoreBundle\Form\FormClientiType.php line 15

已编辑:解决方案:

 public function buildForm(FormBuilderInterface $builder, array $options)
{
   // this was it :) 
    $idSocietate = $options['data']->getSocietati()->getIdSocietate();

    $builder->add('cod','text', array('required' => false));
    $builder->add('denumire','text', array('required' => true));
    $builder->add('cod_fiscal','text', array('required' => true));
    $builder->add('cont_analitic','text', array('required' => false));
    $builder->add('telefon','text', array('required' => false));
    $builder->add('email','email', array('required' => false));
    $builder->add('judet','text', array('required' => false));
    $builder->add('adresa','text', array('required' => false));
    $builder->add('nr_reg_comertului','text', array('required' => false));
    $builder->add('cont','text', array('required' => false));
    $builder->add('banca','text', array('required' => false));
    $builder->add('delegat','text',array('required' => false));
    $builder->add('delegat_serie_ci','text',array('required' => false));
    $builder->add('delegat_nr_ci','text',array('required' => false));
    $builder->add('delegat_ci_eliberat','text',array('required' => false));
    $builder->add('delegat_mij_transport','text',array('required' => false));
    $builder->add('agent','text',array('required' => false));
    $builder->add('discount','text',array('required' => false));

    $builder->add('date_clienti', 'collection', array(
        'type' => new FormDateClientiType($idSocietate),
        'cascade_validation' => true,
        'prototype' => true,
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false,
        'required' => false,
        ));

    $builder->add('adauga','submit');



 }


}

1 个答案:

答案 0 :(得分:1)

也许您正在尝试将null传递给构造函数方法?

我还建议您通过options数组传递$ idSocietate,而不是通过构造函数传递。 options数组是createForm方法的第三个参数。您可以使用$ resolver将选项设置为必需的选项。