在Symfony中使用FormBuilderInterface和{{form(form)}}构建简单的用户创建表单时,将按照我定义的形式创建表单。当我需要添加自定义选择框时,我需要手动创建表单字段。因此我试图使用例如{{form_widget(form.firstname)}},但未创建任何输入字段。
{{ form(form) }}
使用渲染的输入字段。
{{ form_start(form) }}
<div class="form-row">
<div class="form-group col-md-6">
{{ form_widget(form.firstname) }}
</div>
<div class="form-group col-md-6">
{{ form_widget(form.lastname) }}
</div>
</div>
{{ form_end(form) }}
不呈现任何内容。 配置:
$builder
->add('firstname', TextType::class, [
'label' => '',
'attr' => [
'class' => 'form-control',
'placeholder' => 'Vorname'
]
])
->add('lastname', TextType::class, [
'label' => '',
'attr' => [
'class' => 'form-control',
'placeholder' => 'Nachname'
]
]);
任何建议如何解决这个问题或我想念什么?
答案 0 :(得分:0)
这是我学会使用FormBuilderInterface的方法(感谢GRAFIKART教程!)
class PropertyController extends AbstractController
{
/**/
public function __construct(PropertyRepository $repository, ObjectManager $em) {
$this->repository = $repository; $this->em=$em; }
/**
* @Route("/biens", name="property.index")
* @return Response
*/
public function index(PaginatorInterface $paginator, Request $request):Response
{
$search = new PropertySearch();
$form = $this->createForm(PropertySearchType::class, $search);
$form->handleRequest($request);
$properties = $paginator->paginate($this->repository->findAllVisibleQuery($search),
$request->query->getInt('page', 1), 9);
return $this->render('property/index.html.twig', [
'current_menu'=>'properties',
'properties'=>$properties,
'form' => $form->createView()
]);
}
class PropertySearchType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('minSurface', IntegerType::class, [
'required' =>false,
'label' =>false,
'attr'=>['placeholder' => 'Surface minmale']
])
->add('maxPrice', IntegerType::class, [
'required' =>false,
'label' =>false,
'attr'=>['placeholder' => 'Prix maximal']
])
->add('pOptions', EntityType::class,[
'required'=>false,
'label'=>false,
'class'=>POption::class,
'choice_label'=>'name',
'multiple'=>true
] );
//->add('submit', SubmitType::class, ['label' =>'Rechercher' ])
}
以及相应的树枝模板文件:
<div class="container">
{{ form_start(form) }}
<div class='form-row'>
<div class='col'> {{ form_row(form.minSurface) }} </div>
<div class='col'> {{ form_row(form.maxPrice) }} </div>
<div class='col'> {{ form_row(form.pOptions) }} </div>
<div class='col'>
<button class='btn btn-primary'> Rechercher </button>
</div>
</div><!-- end row -->
{{ form_end(form) }}
</div>