Symfony2传递选项以抽象FormType

时间:2014-04-10 16:37:25

标签: php symfony

我已经坚持了很长一段时间了。我在这里读了很多答案,但我无法让它运行。

我的控制器包含

$series = $this->getDoctrine()->getRepository('DemoDemoBundle:Series')->createQueryBuilder('series')
    ->where('series.type = 1')
    ->getQuery()->getResult();
$magazine = new Magazine();
$content = array(
    'series' => $series
);

$form = $this->createForm(new MagazineType($this->get('translator'), $content), $magazine);
$form->handleRequest($request);

我想将变量$content传递给表单,以便'series'可以在某些选择框中使用。 MagazineType延伸PublicationType。底层实体也是如此。以下是MagazineType的外观:

class MagazineType extends PublicationType
{
    function __construct($translator, array $series = null)
    {


 parent::__construct($translator, $series); //$series);
}

/**
 * @param FormBuilderInterface $builder
 * @param array                $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    // TODO: Refactor constant out
    parent::buildForm($builder, $options)
//            ->add('dateSubmitted')
//            ->add('userSubmitted')
//            ->add('file')
        ->add('issueNumber')
        ->add('save', 'submit');
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(
        array(
            'data_class' => 'Demo\DemoBundle\Entity\Magazine',
            'series' => $this->translator->trans('addPublication.noSeries')
        )
    );
}

PublicationType

class PublicationType extends AbstractType
{
    protected $series, $translator;
    public function __construct($translator, $series)
    {
        $this->translator = $translator;
        $this->series = $series;
    }
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder
            ->add('name', 'text', array(
                    'label' => $this->translator->trans('addPublication.name')
                ))
    //            ->add('dateSubmitted')
    //            ->add('userSubmitted')
    //            ->add('file')
            ->add('series', 'choice', $this->series);//$formOptions['series']);
        ;
        return $builder;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
                'data_class' => 'Demo\DemoBundle\Entity\Publication',
                'series' => $this->translator->trans('addPublication.noSeries')
        ));
    }

也许我正在以错误的方式访问系列变量,但我甚至没有进一步构建MagazineType(不允许选项series),即使我已按照{上所述的DefaultOptions设置它{3}}。我可能在这里错过了类似的问题,因为我整天坚持这一点。

感谢您的帮助!

0 个答案:

没有答案