0

我有一个要在我的树枝模板中呈现的集合。但是,我不断从 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->createView()。我怎样才能渲染这个?

4

0 回答 0