dongzhuo1930 2016-06-23 18:01
浏览 6

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

I have two pages and would like to use one uploadType. When I edit, show the name in the textfield. In NewsController I give the data.

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

This is working. But when i will show the uploadPage and set the data in the uploadType.

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

so is the news textfield empty.

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();

//....

I tryed with "addEventListener" in both site. All the same result.

I hope anyone can help me. :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探