When building a simple user creation form in Symfony with FormBuilderInterface and {{ form(form) }} my form is created as i defined it. While i need to add custom select boxes i need to create the form field manually. Therefor i tried to use e.g. {{ form_widget(form.firstname) }} but no input field is created.
{{ form(form) }}
Working with rendered input fields.
{{ 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) }}
not rendering anything. Configuration:
$builder
->add('firstname', TextType::class, [
'label' => '',
'attr' => [
'class' => 'form-control',
'placeholder' => 'Vorname'
]
])
->add('lastname', TextType::class, [
'label' => '',
'attr' => [
'class' => 'form-control',
'placeholder' => 'Nachname'
]
]);
Any advice how to solve this or what do i miss?
Here is the way I have learnt to use the FormBuilderInterface (thank to GRAFIKART tutorial !)
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' ])
}
And the corresponding twig-template file :
<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>