预填充未映射字段的表单值

时间:2022-03-30 10:29:01

I have a form that is bound to an entity, but it also has an extra unmapped field: (from the FormType class)

我有一个绑定到实体的表单,但它还有一个额外的未映射字段:(来自FormType类)

$builder
    ->add('name')
    ->add('qoh')
    ->add('serialNumber', 'text', array('mapped' => false, 'required' => false))

I want to pre-populate the serialNumber field from the controller with information taken from the request URL. The closest method I have found would be:

我想从控制器预先填充serialNumber字段,其中包含从请求URL获取的信息。我发现的最接近的方法是:

$form->setData(mixed $modelData)

$ form-> setData(mixed $ modelData)

but the API does not specify what form '$modelData' takes and nothing I've tried has had any effect.

但API没有指定'$ modelData'采用什么形式,我尝试过的任何东西都没有任何效果。

3 个解决方案

#1


31  

Someone on Symfony's IRC channel gave me this answer, and they declined to post it here:

Symfony的IRC频道有人给了我这个答案,他们拒绝在这里发布:

$form->get('serialNumber')->setData($serial_number);

$形式 - >获取( 'SERIALNUMBER') - >使用setData($ SERIAL_NUMBER);

#2


7  

You can pre-populate the field in twig (Set default value of Symfony 2 form field in Twig).

您可以在树枝中预先填充该字段(在Twig中设置Symfony 2表单字段的默认值)。

...

{{ form_widget(form.serialNumber, { value : serialNumber }) }}

...

#3


7  

You can use Form Events. For example if you want to set the data from the database to a non mapped field you can use POST_SET_DATA:

您可以使用表单事件。例如,如果要将数据从数据库设置为非映射字段,则可以使用POST_SET_DATA:

class AddNonMappedDataSubscriber implements EventSubscriberInterface
{
protected $em;

public function __construct(EntityManager $em)
{
    $this->em = $em;
}

public static function getSubscribedEvents()
{
    return array(
        FormEvents::POST_SET_DATA => 'postSetData'
    );
}

public function postSetData(FormEvent $event){
    $form = $event->getForm();
    $myEntity = $event->getData();

    if($myEntity){
        $serialNumber = $myEntity->getNumber();
        $form->get('serialNumber')->setData($serialNumber);
         }
     }
}

#1


31  

Someone on Symfony's IRC channel gave me this answer, and they declined to post it here:

Symfony的IRC频道有人给了我这个答案,他们拒绝在这里发布:

$form->get('serialNumber')->setData($serial_number);

$形式 - >获取( 'SERIALNUMBER') - >使用setData($ SERIAL_NUMBER);

#2


7  

You can pre-populate the field in twig (Set default value of Symfony 2 form field in Twig).

您可以在树枝中预先填充该字段(在Twig中设置Symfony 2表单字段的默认值)。

...

{{ form_widget(form.serialNumber, { value : serialNumber }) }}

...

#3


7  

You can use Form Events. For example if you want to set the data from the database to a non mapped field you can use POST_SET_DATA:

您可以使用表单事件。例如,如果要将数据从数据库设置为非映射字段,则可以使用POST_SET_DATA:

class AddNonMappedDataSubscriber implements EventSubscriberInterface
{
protected $em;

public function __construct(EntityManager $em)
{
    $this->em = $em;
}

public static function getSubscribedEvents()
{
    return array(
        FormEvents::POST_SET_DATA => 'postSetData'
    );
}

public function postSetData(FormEvent $event){
    $form = $event->getForm();
    $myEntity = $event->getData();

    if($myEntity){
        $serialNumber = $myEntity->getNumber();
        $form->get('serialNumber')->setData($serialNumber);
         }
     }
}