我在Symfony 3.2.7控制器中有以下方法:
public function updateAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
// this is the original entity without modified values
$entity = $em->getRepository('QuoteBundle:Quote')->find($request->query->get('id'));
// the entity passed to the form has the values modified coming from the request
$form = $this->createForm(CFProgramLevelQuoteType::class, $entity);
...
}
这就是形式:
class CFProgramLevelQuoteType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('CFProgramLevel', EntityType::class, [
'class' => 'QuoteBundle:CFProgramLevel',
'choice_label' => 'description',
'placeholder' => 'Please select a program',
]);
}
...
}
我将使用查询生成器过滤QuoteBundle:CFProgramLevel
中的某些值,因此我需要从$entity
获取未修改的ID并将其发送到表单。这是我到目前为止所考虑的解决方案:
public function updateAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('QuoteBundle:Quote')->find($request->query->get('id'));
$entity_id = $entity->getCfProgramLevel()->getcfProgramLevelId();
$form = $this->createForm(CFProgramLevelQuoteType::class, $entity);
...
}
但是我如何将entity_id
传递给表单并在那里使用它?如果有更好的方法来实现这一目标,我愿意听取意见。我在谷歌找不到任何有用的东西,所以任何帮助都表示赞赏。
更新
尝试下面答案中提供的解决方案我无法使其工作:
// controller
$form = $this->createForm(CFProgramLevelQuoteType::class, $entity, ['entity_id' => $entity_id]);
// form
public function buildForm(FormBuilderInterface $builder, array $options)
{
dump($options);
die();
...
}
public function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => Quote::class, 'entity_id' => null]);
$resolver->setRequired('entity_id');
}
选项“entity_id”不存在。定义的选项是:“行动”, “allow_extra_fields”,“attr”,“auto_initialize”,“block_name”, “by_reference”,“compound”,“constraints”,“csrf_field_name”, “csrf_message”,“csrf_protection”,“csrf_token_id”, “csrf_token_manager”,“data”,“data_class”,“disabled”,“empty_data”, “error_bubbling”,“error_mapping”,“extra_fields_message”, “inherit_data”,“invalid_message”,“invalid_message_parameters”, “label”,“label_attr”,“label_format”,“mapped”,“method”, “post_max_size_message”,“property_path”,“required”, “translation_domain”,“trim”,“upload_max_size_message”, “validation_groups”。
答案 0 :(得分:1)
您可以为表单类型创建自己的自定义选项,如http://symfony.com/doc/current/form/create_custom_field_type.html#defining-the-field-type
中所述class CFProgramLevelQuoteType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
//$options['entity_id'] contains your id
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('entity_id');
}
}
传递entity_id:
$form = $this->createForm(CFProgramLevelQuoteType::class, $entity, [
'entity_id' => $entity_id
]);
答案 1 :(得分:1)
这是完全有效的例子(非常类似于@striker)。如果您没有拼错,请仔细检查。否则请尝试php bin/console cache:clear
控制器动作:
public function testAction(Request $request)
{
$form = $this->createForm(TestType::class, null, [
'option1' => 6
]);
return $this->render('default/test.html.twig',[
'form' => $form->createView()
]);
}
TestType.php
:
class TestType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('test', IntegerType::class, [
'data' => $options['option1']
]);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('option1');
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_test';
}
}