setData与嵌入的EntityType无法正常工作

时间:2016-06-23 18:01:30

标签: php symfony

我有两个页面,想要使用一个uploadType。编辑时,在文本字段中显示名称。在NewsController中我给出了数据。

$ EditForm ['upload'] ['filename'] -> setData ($ upload); 

这很有效。但是当我将显示uploadPage并在uploadType中设置数据时。

'data' => $ build-> getData () 

新闻文本字段也是空的。

UploadType:

class UploadType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{

    $data = $builder->getData();


    if($this->isFieldEnabled('directory',$options)) {
        $builder ->add('directory', null, array(
                'label' => 'upload.directory',
                'required' => true,
            )
        );
    }

    $builder->add('name', FileType::class,array(
        'label' => 'upload.choice',
        'data_class' => null,
        'attr' => array(
            'class' => 'hidden'
        )
    ));

    if($this->isFieldEnabled('filename',$options)) {
        $builder->add('filename', 'text',array(
            'label' => 'upload.upload',
            'data' => $data, //If set, don't show in the news textfield
            'mapped' => false,
        ));
    }

}


/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        array(
            'data_class' => 'Maiskolben\UploadBundle\Entity\Upload',
            'translation_domain' => 'Maiskolben',
            'disableFields' => [],
        )
    );

    $resolver->setOptional([
        'disableFields',
    ]);
}

private function isFieldEnabled($fieldName, $options)
{
    if (isset($options['disableFields']) && !empty($options['disableFields'])) {
        if (in_array($fieldName, $options['disableFields'])) {
            return false;
        }
    }

    return true;
}

}

NewsType:

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

    $builder
        ->add('title', TextType::class,array(
            'label' => 'news.newsTitle',
        ))

        ->add('content','textarea',array(
            'label' => 'news.content',
        ))

        ->add('upload', new UploadType(), array(
            'label' => false,
            'required' => false,
            'disableFields' => array(
                'directory',
            )
        ));
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Maiskolben\NewsBundle\Entity\News',
        'translation_domain' => 'Maiskolben',
    ));
}
}

NewsController:

public function editAction(Request $request, News $news)
{
    $upload = $news->getUpload();

    $editForm = $this->createForm(new NewsType, $news);
    $editForm['upload']['filename']->setData($upload);
    $editForm->handleRequest($request);
    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $em = $this->getDoctrine()->getManager();

//....

我在两个网站都尝试使用“addEventListener”。所有相同的结果。

我希望有人能帮助我。 :)

0 个答案:

没有答案