symfony 表单:提交后无法映射复合字段

symfony form: cannot map compound field after submission

提问人:Parktrip 提问时间:10/31/2023 更新时间:10/31/2023 访问量:14

问:

我的问题是提交表单后没有映射复合字段。我试图用以下类来简化我的情况:

class Article
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy:"SEQUENCE")]
    #[ORM\SequenceGenerator(sequenceName:"SEQ_ERN_ARTICLE")]
    #[ORM\Column(type: 'integer', name:'ARTICLE_ID')]
    private int $id;

    #[ORM\Column(type:"string", length:255, name:'ARTICLE_LABEL')]
    private string $label;

    #[ORM\Column(type: 'blob', nullable: true, name: 'ARTICLE_PICTURE_CONTENT')]
    private $pictureContent = null;

    #[ORM\Column(type: 'blob', nullable: true, name: 'ARTICLE_THUMBNAIL_CONTENT')]
    private $thumbnailContent = null;

    //non-doctrine attribute to store the Picture 
    private ?Picture $picture = null;
}

我的表格

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('label', TextType::class, [
                'empty_data' => '',
                'priority' => 80,
            ])
            ->add('picture', PictureType::class, [
                'required' => false,
                'mapped' => true,

            ])
            ->add('submit', SubmitType::class, [
                'priority' => 0
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Article::class,
        ]);
    }
}

处理提交的控制器部分:

$form = $this->createForm(ArticleType::class, $article);

        $form->handleRequest($request);

        if ($form->isSubmitted()) {
 
            if ($form->isValid()) {
                dump($form->get('picture')->get('filename')->getData());
                $article = $form->getData();
    
                $pictureUploader->upload($form);
                $articleRepo->save($article, true);
            }
        }

dump() 命令返回 null,而在分析器中,在“请求/响应”中有一个“上传的文件”部分,其中包含以下信息:

Symfony profiler

在我的 PictureType 类中,我尝试按照此处的建议使用 DataMapper:https://symfony.com/doc/current/form/data_mappers.html 但它不起作用。

下面是 PictureType 类

<?php 
namespace App\Form\Type;

use App\Picture;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PictureType extends AbstractType implements DataMapperInterface
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'compound' => true,
        ]);
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('filename', FileType::class)
            ->add('cropInfo', HiddenType::class)
            ->setDataMapper($this);
    }

    public function getParent()
    {
        return FileType::class;
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'picturetest';
    }

     /**
     * @param Picture|null $viewData
     */
    public function mapDataToForms($viewData, \Traversable $forms): void
    {
        dump($viewData, 'mapDataToForms');
        // there is no data yet, so nothing to prepopulate
        if (null === $viewData) {
            return;
        }
        dump('mapDataToForms -- 2'); //never reached

        // invalid data type
        if (!$viewData instanceof Picture) {
            throw new UnexpectedTypeException($viewData, Picture::class);
        }

        /** @var FormInterface[] $forms */
        $forms = iterator_to_array($forms);

        dump($viewData->getFile());
        // initialize form field values
        $forms['filename']->setData($viewData->getFile());
        $forms['cropInfo']->setData($viewData->getCropInfo());
    }

    public function mapFormsToData(\Traversable $forms, &$viewData): void
    {
        /** @var FormInterface[] $forms */
        $forms = iterator_to_array($forms);

        // as data is passed by reference, overriding it will change it in
        // the form object as well
        $viewData = new Picture(
            $forms['filename']->getData(),
            $forms['cropInfo']->getData()
        );
        dump($forms, 'mapFormsToData');
    }
}

调试字符串“mapDataToForms -- 2”永远不会被触及。

我做错了什么?

PHP 表单 symfony

评论


答: 暂无答案