Answer the question
In order to leave comments, you need to log in
How to correctly process a form that is not associated with an Entity?
Made the form not related to the entity. In fact, this is a simple form of sorting records in the admin panel (in the future I will add filtering by parameters).
class ImageFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('sortBy', ChoiceType::class, [
'choices' => [
'ID' => 'id',
'label.createdAt' => 'createdAt',
'label.publishedAt' => 'publishedAt',
'label.updatedAt' => 'updatedAt',
],
'label' => 'label.sort_by',
'required' => true,
])
->add('direction', ChoiceType::class, [
'choices' => [
'label.sort_direction_ASC' => 'ASC',
'label.sort_direction_DESC' => 'DESC'
],
'label' => 'label.sort_direction',
'required' => true,
])
->add('submit', SubmitType::class, [
'label' => 'OK'
]);
}
}
/**
* @Route("/images/{page}", defaults={"page": 1}, name="admin_image_list")
*/
public function index(int $page, Request $request, ImageRepository $imageRepository): Response
{
$sortBy = $request->get('sortBy', 'id');
$direction = $request->get('direction', 'DESC');
$filterForm = $this->createForm(ImageFilterType::class, [
'sortBy' => $sortBy,
'direction' => $direction,
])->handleRequest($request);
$images = $imageRepository->findEntities([], $sortBy, $direction, $page, 10);
return $this->render('admin/gallery/image_list.html.twig', [
'images' => $images,
'filterForm' => $filterForm->createView(),
]);
}
image_filter [▼
"sortBy" => "updatedAt"
"direction" => "DESC"
"submit" => ""
"_token" => "dOketsGIJFAVlVaPvjbBtzx5b3ksQ8nZ6jwY-JT7HDo"
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question