Symfony表单错误:应在属性路径中给出类型为“string”的参数,即“App\Entity\User”

pnwntuvh  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(153)
$builder
    ->add('ophaal_locatie')
    ->add('lesdoel')
    ->add('naam_leerling')
    ->add('naam_instructeur', null, [
        'data' => $user->getEmail(),
        'label' => false,
        'attr' => [
            'class' => 'my-custom-class',
            'style' => 'display: none;',
        ],
    ])

    ->add('datum')
    ->add('prijs')
    ->add('Leerling', 'NonexistentClass', [
        'class' => User::class, 
        'choice_label' => 'username', field
    ]);

我正在处理Symfony表单,遇到错误。错误消息为:“预期参数类型为'string','App\Entity\User'在属性路径'naam_instructeur '给出。”我正在尝试将'naam_instructeur'字段的默认值设置为登录用户的电子邮件。
这是相关代码

rqenqsqc

rqenqsqc1#

NinjaCoder69试试看:

use App\Entity\Lessen;
use Proxies\__CG__\App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class LessenType extends AbstractType
{
    private $tokenStorage;

    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $user = $this->tokenStorage->getToken()->getUser();


        $builder
            // STATUS EERST OPTIONEEL MAKEN IN DB (Als het staat op tecnhnisch ontwerp docenten)
//            ->add('status', null, [
//                'empty_data' => "\xC2\xA0",
//            ])

            ->add('ophaal_locatie')
            ->add('lesdoel')
            ->add('naam_leerling')
            ->add('naam_instructeur', null, [
                'data' => $user->getEmail(),
                'label' => false,
                'attr' => [
                    'class' => 'my-custom-class',
                    'style' => 'display: none;',
                ],
            ])

            ->add('datum')
            ->add('prijs')
            ->add('Leerling', EntityType::class, [
                'class' => User::class, 
                'choice_label' => 'username', 
            ])
        ;
    }
cmssoen2

cmssoen22#

->add('Leerling', EntityType::class, [
    'class' => User::class, 
    'choice_label' => 'username', 
])

相关问题