Symfony2 - 如何设置实体字段类型的默认选项?

时间:2016-03-26 20:49:40

标签: forms symfony

使用 entity 字段类型时,如何预先选择

我想预先选择从数据库中提取的其中一个国家/地区,因为我们的大多数用户都位于同一个国家/地区。

我可以通过在过去添加checkbox选项来预先选择data字段中的值。

此LocationType表单包含在UserType表单中,因为表单是从多个实体构建的。

LocationType表单:

namespace Cmp\MyBundle\Form\Type;

use Cmp\MyBundle\Entity\Account;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\EntityRepository;

    class LocationType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {

            $builder->add('company')
                ->add('street')
                ->add('streetno')
                ->add('streetsuffix')
                ->add('zipcode')
                ->add('city')
                ->add('province')
                ->add('country', 'entity', array(
                    'class' => 'CmpMyBundle:Country',
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                            ->orderBy('c.nameEn', 'ASC');
                    },
                    'choice_label' => 'nameEn',
                ))
                ->add('tel')
                ->add('fax')
                ->add('url')
                ->add('account', new AccountType());
        }

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Cmp\MyBundle\Entity\Location',
            ));
        }

        public function getName()
        {
            return 'location';
        }
    }

Herby the twig to betry list dorpdown。

       <div class="form-group {% if not form.location.country.vars.valid %}has-error{% endif %}">
            {{ form_label(form.location.country, 'country'|trans, {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
            <div class="col-sm-9">
                {{ form_errors(form.location.country) }}
                {{ form_widget(form.location.country, {'attr': {'class': 'form-control', 'placeholder': 'country'|trans}}) }}
            </div>
        </div>

生成表单的控制器:

    $user = new User();
    $form = $this->createForm(new UserRegType(), $user);

用户类型表单包含上面显示的位置表单类型。

2 个答案:

答案 0 :(得分:0)

创建表单时,您可以为该字段分配值

例如:

$data = new Foo();
$data->setCountryCode($country);
$form = $this->createForm(new myFormType(), $data);

答案 1 :(得分:0)

试试这个:

namespace Cmp\MyBundle\Form\Type;

use Cmp\MyBundle\Entity\Account;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\EntityRepository;

class LocationType extends AbstractType
{
    protected $er;

    public function __construct(EntityRepository $er) 
    {
        $this->er = $er;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('company')
            ->add('street')
            ->add('streetno')
            ->add('streetsuffix')
            ->add('zipcode')
            ->add('city')
            ->add('province')
            ->add('country', 'entity', array(
                'class' => 'CmpMyBundle:Country',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('c')
                        ->orderBy('c.nameEn', 'ASC');
                },
                'data' => $this->er->FindOneBy(['country_code' => 'nl'])
                'choice_label' => 'nameEn',
            ))
            ->add('tel')
            ->add('fax')
            ->add('url')
            ->add('account', new AccountType());
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Cmp\MyBundle\Entity\Location',
        ));
    }

    public function getName()
    {
        return 'location';
    }
}

您需要在服务定义中注入国家/地区仓库:

services:
    app.country_repository:
        class: Doctrine\ORM\EntityRepository
        factory_service: doctrine.orm.default_entity_manager
        factory_method: getRepository
        arguments:
            - YOUR\COUNTRY\NAMESPACE\Country
    app.form.type.location:
        class: Cmp\MyBundle\Form\Type\LocationType
        arguments:
            - "@app.country_repository"
        tags:
            - { name: form.type }