我有一个简单的模型(源代码的简化):
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
)
问题:如何更改表单行为,使其忽略未提交的值?