Symfony 4类型:选项“0”不存在

webghufk  于 2023-03-30  发布在  其他
关注(0)|答案(4)|浏览(129)

我找到了一个与我的问题类似的答案,但我不明白它是如何工作的,我应该在我的代码中更改什么。所以我向您寻求帮助,我有这个错误消息:
选项“0”不存在。定义的选项有:“action”,“allow_extra_fields”,“allow_file_upload”,“attr”,“auto_initialize”,“block_name”,“by_reference”,“compound”,“constraints”,“csrf_field_name”,“csrf_message”,“csrf_protection”,“csrf_token_id”,“csrf_token_manager”,“data”,“data_class”,“disabled”,“empty_data”,“error_bubbling”,“error_mapping”,“extra_fields_message”,“help”、“help_attr”、“inherit_data”、“invalid_message”、“invalid_message_parameters”、“label”、“label_attr”、“label_format”、“mapped”、“method”、“post_max_size_message”、“property_path”、“required”、“translation_domain”、“trim”、“upload_max_size_message”、“validation_groups”。
下面是我的类型:

<?php

namespace App\Form;

use App\Entity\Equipment;
use App\Entity\Property;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PropertyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('propertyCategory', ChoiceType::class, [
                'required' => true,
                'choices' => [
                    'Appartement' => 'Appartement',
                    'Maison' => 'Maison',
                    'Garage' => 'Garage',
                    'Bureau' => 'Bureau',
                    'Château' => 'Château',
                    'Commerce' => 'Commerce',
                ]
            ])
            ->add('uniqueName', TextType::class, ['required' => true, 'label' => 'Nom unique'])
            ->add('address', TextType::class, ['required' => true, 'label' => 'Adresse'])
            ->add('city', TextType::class, ['required' => true, 'label' => 'Ville'])
            ->add('zipcode', IntegerType::class, ['required' => true, 'label' => 'Code postal'])
            ->add('country', CountryType::class, ['required' => true, 'label' => 'Pays'])
            ->add('surfaceInSquareMeter', IntegerType::class, [
                'required' => true,
                'label' => 'Surface en m²'
            ])
            ->add('numberOfPiece', IntegerType::class, ['required' => true, 'label' => 'Nombre de pièces'])
            ->add('description', TextType::class, ['required' => false, 'Description'])
            ->add('equipments', EntityType::class, [
                'required' => false,
                'label' => 'Equipements',
                [
                    'class' => Equipment::class,
                    'multiple' => true,
                    'expanded' => true,
                    'choice_label' => 'name'
                ]
            ])
            ->add('locationCategory', ChoiceType::class, [
                'required' => true,
                'choices' => [
                    'Meublé' => 'Meublé',
                    'Non meublé' => 'Non meublé',
                ]
            ])
            ->add('rentExcludingCharges', NumberType::class, [
                'required' => true,
                'label' => 'Loyer hors charges'
            ])
            ->add('charges', NumberType::class, ['required' => true, 'label' => 'Charges'])
            ->add('purchasePrice', NumberType::class, ['required' => true, 'label' => 'Prix d\'achat']);
    }

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

希望你能理解我的英语,有人可以帮助我(并解释我如何避免再犯这个错误)
谢谢
编辑:有一个问题与那些无用的[]在

->add('equipments', EntityType::class, [
            'required' => false,
            'label' => 'Equipements',
            [
                'class' => Equipment::class,
                'multiple' => true,
                'expanded' => true,
                'choice_label' => 'name'
            ]
        ])

,我解决了它,但错误消息是相同的

klr1opcd

klr1opcd1#

此处缺少标签属性:

->add('description', TextType::class, ['required' => false, 'Description'])
dohp0rv5

dohp0rv52#

只是有同样的问题,但在我的情况下,这是因为我有一个空的选项在我的形式...可能会帮助别人在未来:)

$builder
    ->add('prijsPerVertoning', MoneyType::class, [
        'currency' => 'EUR',
        'divisor'  => 1000,
        '' // <---- this breaks functionality
    ])->add('submit', SubmitType::class);
qv7cva1a

qv7cva1a3#

我觉得你的[]太少了:

->add('equipments', EntityType::class, [
            'required' => false,
            'label' => 'Equipements',
            //[
                'class' => Equipment::class,
                'multiple' => true,
                'expanded' => true,
                'choice_label' => 'name'
            //]
        ])
6gpjuf90

6gpjuf904#

SYMFONY 6.2
当构建表单时,

$form = $this->createForm(YourFormType::class, $classData, ['']);

并如上传递第三个参数
选项0不存在
如果你在symfony中传递第三个参数作为[''],直到6.1 -它都能工作,从6.2开始就不行了。

相关问题