如何在 Symfony3 表单中设置默认国家?

ras*_*han 1 php symfony-forms symfony

我正在使用 Symfony3 形式。CountryType我的 Symfony 表单生成器中有一个。它工作正常。但假设用户属于一家位于西班牙的公司。对于该用户,我想将默认国家/地区设置为西班牙,然后显示其余国家/地区。我怎样才能在 Symfony3 中做到这一点。

我试过这个,但它不工作。

        $builder->add("country", CountryType::class, array(
            "label" => "Country",
            "required" => false,
            "preferred_choices" => array(
                "ES" => "Spain",
            ),
        ));
Run Code Online (Sandbox Code Playgroud)

感谢您的时间。

小智 5

在这种类型中Country::class,要在数组上使用首选选择,您应该使用:

->add('country', CountryType::class, [
    'preferred_choices' => ['DE'],
    'label' => 'address.form.country.label',
    'attr' => [
        'class' => 'form-control',
        'placeholder' => 'address.form.country.placeholder'
    ],
    'label_attr' => [
        'class' => 'col-sm-2 col-form-label'
    ],
])
Run Code Online (Sandbox Code Playgroud)