如何在树枝中以嵌入的形式渲染字段?

时间:2013-09-02 17:32:11

标签: php forms symfony twig

我有一个我想在我的树枝模板中渲染的集合。但是我继续从twig得到这个ContextErrorException,暗示它没有得到我的表单的表单视图。在我的控制器中,我正在传递表格......

'formExperience' => $formExperience->createView()

下面显示formExperience表格。

<?php
// Form/CareExperienceForm

namespace Caremonk\MainSiteBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Caremonk\MainSiteBundle\Form\CareWorkExperienceForm;

class CareExperienceForm extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('experience','collection',array(
            'type' => new CareWorkExperienceForm(),
            'allow_add' => true,
            'allow_delete' => true));
        $builder->add('AddExperience','button', array(
            'attr' => array(
                'class'=>'experience'
                )
            ));
        $builder->add('SaveInfo','button', array(
            'attr' => array(
                'class' => 'save'
                )
            ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        // Do nothing for the time being
        $resolver->setDefaults(array(
            'data_class' => 'Caremonk\MainSiteBundle\Processors\CareExperienceProcessor'
        ));
    }

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

我正在尝试渲染'体验'对象的独立字段,该类如下所示:

<?php
// Form/CareExperienceForm

namespace Caremonk\MainSiteBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Caremonk\MainSiteBundle\Entity\WorkExperience;

class CareWorkExperienceForm extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('companyClient','text',
            array('required' => true,'label'  => 'company / client name',
            'attr'=> array(
                'class'=>'care_giver_db_register-form-experience-company textbox',
                'placeholder'=>'compnay name',
                'field'=>'companyClient'),
            'label_attr'=> array('class'=>'care_giver_db_register-form-experience-title-company')
            ));

        $builder->add('position','text',
            array('required' => true,'label'  => 'position',
            'attr'=> array(
                'class'=>'care_giver_db_register-form-experience-position textbox',
                'placeholder'=>'position held',
                'field'=>'position'),
            'label_attr'=> array('class'=>'care_giver_db_register-form-experience-title-position')
            ));

        $builder->add('description','textarea',
            array('required' => true,'label'  => 'description',
            'attr'=> array(
                'class'=>'care_giver_db_register-form-experience-description textarea',
                'placeholder'=>'test content',
                'field'=>'description'),
            'label_attr'=> array('class'=>'care_giver_db_register-form-experience-title-description')
            ));

        $builder->add('startDate','date',
            array('required' => true,'label'  => 'start date',
            'attr'=> array(
                'class'=>'care_giver_db_register-form-experience-start date',
                'field' => 'startDate'),
            'label_attr'=> array('class'=>'care_giver_db_register-form-experience-title-start')
            ));

        $builder->add('endDate','date',
            array('required' => true,'label'  => 'end date',
            'attr'=> array(
                'class'=>'care_giver_db_register-form-experience-end date',
                'field'=>'endDate'),
            'label_attr'=> array('class'=>'care_giver_db_register-form-experience-title-end')
            ));

        $builder->add('currentlyWorkingHere','checkbox',
            array('required' => true,'label'  => 'i currently work here',
            'attr'=> array(
                'class'=>'care_giver_db_register-form-experience-current checkbox',
                'p-true'=>'yes',
                'p-false'=>'no',
                'field'=>'currentlyWorkHere'),
            'label_attr'=> array('class'=>'care_giver_db_register-form-experience-title-current')
            ));

        $builder->add('ClearInfo','reset',array(
            'attr' => array('class' => 'care_giver_db_register-form-experience-cancel-collection'
                )
            ));


        $builder->add('DeleteField','button', array(
            'attr' => array('class' => 'care_giver_db_register-form-experience-delete-collection deleteForm'),
            ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        // Do nothing for the time being
        $resolver->setDefaults(array(
            'data_class' => 'Caremonk\MainSiteBundle\Entity\WorkExperience'
        ));
    }

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

在我的视图中,我正在执行以下操作,这会给我错误:

{% for experience in workExperience.experience %}
{{ experience.companyClient }}
{% endfor %}
然而,

Symfony抱怨在没有获取表单视图时抛出的上下文错误异常。例如,它就像传递常规表单而不在symfony中执行form-&gt; createView()。我该如何渲染呢?

0 个答案:

没有答案