You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony表单提交报错:属性路径name期望string却传入App\Entity\Location

Symfony表单类型不匹配问题解决

错误信息

Expected argument of type "string", "App\Entity\Location" given at property path "name".

提交表单时触发上述类型不匹配错误,尝试修改字段名为location、设置data_class均未解决问题。

涉事代码

class McsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', EntityType::class,[ 
                'class' => Location::class,
                'label' => 'For The Location',
                'choice_label' => function(Location $location){
                    return $location->getName();
                }
            ])
            
            ->add('mcs', CollectionType::class,[ 
                'entry_type'=> McType::class,
                'entry_options' => ['label' => false ],
                'by_reference' => false,
                'allow_add' => true,
                'allow_delete' => true
            ]);
        ;
    }

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

错误原因

表单中name字段使用EntityType,提交后会返回Location对象,但表单绑定的实体中,name属性是字符串类型,导致类型不匹配。即使改字段名为location,若实体中没有对应的Location类型属性,或未正确配置data_class绑定实体,错误依然存在。

解决方案

场景1:实体需要关联Location对象(多对一/一对一关联)

如果业务逻辑是存储Location实体关联,而非单纯的名称字符串:

  1. 修正实体属性
    在表单绑定的实体类中,添加Location类型的属性及对应的getter/setter:

    namespace App\Entity;
    
    use App\Entity\Location;
    use Doctrine\ORM\Mapping as ORM;
    
    class YourEntity // 替换为实际实体类名
    {
        // ... 其他属性
    
        #[ORM\ManyToOne(targetEntity: Location::class)]
        #[ORM\JoinColumn(nullable: false)]
        private ?Location $location = null;
    
        public function getLocation(): ?Location
        {
            return $this->location;
        }
    
        public function setLocation(?Location $location): self
        {
            $this->location = $location;
            return $this;
        }
    }
    
  2. 修改表单字段配置
    将表单字段名改为实体中对应的关联属性名location,并设置正确的data_class:

    class McsType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options): void
        {
            $builder
                ->add('location', EntityType::class,[ 
                    'class' => Location::class,
                    'label' => 'For The Location',
                    'choice_label' => function(Location $location){
                        return $location->getName();
                    }
                ])
                // ... 其他字段
            ;
        }
    
        public function configureOptions(OptionsResolver $resolver): void
        {
            $resolver->setDefaults([
                'data_class' => YourEntity::class, // 替换为实际实体类
            ]);
        }
    }
    
  3. 控制器中绑定实体
    创建表单时传入实体对象:

    $entity = new YourEntity();
    $form = $this->createForm(McsType::class, $entity);
    $form->handleRequest($request);
    

场景2:实体仅需存储Location名称(字符串)

如果业务只需要存储Location的名称字符串,改用ChoiceType替代EntityType:

  1. 注入LocationRepository
    表单类中注入仓库类,用于获取所有Location数据:
    namespace App\Form;
    
    use App\Entity\Location;
    use App\Repository\LocationRepository;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
    
    class McsType extends AbstractType
    {
        private $locationRepository;
    
        public function __construct(LocationRepository $locationRepository)
        {
            $this->locationRepository = $locationRepository;
        }
    
        public function buildForm(FormBuilderInterface $builder, array $options): void
        {
            // 生成Location名称选项
            $locations = $this->locationRepository->findAll();
            $locationChoices = [];
            foreach ($locations as $location) {
                $locationChoices[$location->getName()] = $location->getName();
            }
    
            $builder
                ->add('name', ChoiceType::class,[ 
                    'choices' => $locationChoices,
                    'label' => 'For The Location',
                ])
                // ... 其他字段
            ;
        }
    
        public function configureOptions(OptionsResolver $resolver): void
        {
            $resolver->setDefaults([
                'data_class' => YourEntity::class, // 替换为实际实体类
            ]);
        }
    }
    

内容的提问来源于stack exchange,提问作者Demi God

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 02:44:58