Вот как я научился использовать интерфейс FormBuilderInterface (спасибо учебнику GRAFIKART!)
ФАЙЛ: src / Controller / Property.php
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()
]);
}
ФАЙЛ: src / Form / PropertySearchType.php
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' ])
}
И соответствующий файл шаблона ветки:
ФАЙЛ: src / templates / property / index.html.twig
<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>