5

我有一个简单的模型(源代码的简化):

class Collection
{
    public $page;
    public $limit;
}

和一个表单类型:

class CollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('page', 'integer');
        $builder->add('limit', 'integer');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FSC\Common\Rest\Form\Model\Collection',
        ));
    }
}

我的控制器:

public function getUsersAction(Request $request)
{
    $collection = new Collection();
    $collection->page = 1;
    $collection->limit = 10;

    $form = $this->createForm(new CollectionType(), $collection)
    $form->bind($request);

    print_r($collection);exit;
}

当 iPOST /users/?form[page]=2&form[limit]=20时,响应是我所期望的:

Collection Object
(
    [page:public] => 2
    [limit:public] => 20
)

现在,当 i 时POST /users/?form[page]=3,响应是:

Collection Object
(
    [page:public] => 3
    [limit:public] =>
)

limit变为 null,因为它没有被提交。

我想得到

Collection Object
(
    [page:public] => 3
    [limit:public] => 10 // The default value, set before the bind
)

问题:如何更改表单行为,使其忽略未提交的值?

4

2 回答 2

10

如果只是参数(GET参数)的问题,您可以将默认值定义到路由文件中

route_name:
pattern: /users/?form[page]={page}&form[limit]={limit}
defaults: { _controller: CompanyNameBundleName:ControllerName:ActionName, 
                         limit:10 }

另一种方法是使用钩子(即PRE_BIND)并将该值手动更新到此事件中。这样,您就没有将“逻辑”传播到多段代码中。

最终代码 - 由 Adrien 建议 - 将是

<?php

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;

class IgnoreNonSubmittedFieldSubscriber implements EventSubscriberInterface
{
    private $factory;

    public function __construct(FormFactoryInterface $factory)
    {
        $this->factory = $factory;
    }

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

    public function preBind(FormEvent $event)
    {
        $submittedData = $event->getData();
        $form = $event->getForm();

        // We remove every child that has no data to bind, to avoid "overriding" the form default data
        foreach ($form->all() as $name => $child) {
            if (!isset($submittedData[$name])) {
                $form->remove($name);
            }
        }
    }
}
于 2012-07-27T12:34:43.167 回答
2

这是对原始答案的修改。这个解决方案最重要的好处是验证器现在可以表现得好像表单发布总是完整的,这意味着没有错误冒泡等问题。

请注意,对象字段名称必须与表单字段名称相同,此代码才能正常工作。

<?php
namespace Acme\DemoBundle\Form;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;

class FillNonSubmittedFieldsWithDefaultsSubscriber implements EventSubscriberInterface
{
    private $factory;

    public function __construct(FormFactoryInterface $factory)
    {
        $this->factory = $factory;
    }

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

    public function preBind(FormEvent $event)
    {
        $submittedData = $event->getData();
        $form = $event->getForm();

        // We complete partial submitted data by inserting default values from object
        foreach ($form->all() as $name => $child) {
            if (!isset($submittedData[$name])) {
                $obj = $form->getData();

                $getter = "get".ucfirst($name);
                $submittedData[$name] = $obj->$getter();
            }
        }
        $event->setData($submittedData);

    }
}
于 2013-05-13T12:47:05.433 回答