|
2 | 2 |
|
3 | 3 | namespace App\Controller; |
4 | 4 |
|
5 | | -use App\Form\Type\AutocompleteSelectType; |
| 5 | +use App\Form\FruitAutocompleteField; |
6 | 6 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 7 | +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
| 8 | +use Symfony\Component\HttpFoundation\Response; |
7 | 9 | use Symfony\Component\Routing\Attribute\Route; |
8 | 10 |
|
9 | 11 | #[Route('/ux-autocomplete')] |
10 | 12 | final class AutocompleteController extends AbstractController |
11 | 13 | { |
12 | 14 | #[Route('/without-ajax')] |
13 | | - public function index() |
| 15 | + public function withoutAjax(): Response |
14 | 16 | { |
15 | | - $form = $this->createForm(AutocompleteSelectType::class); |
| 17 | + $formBuilder = $this->createFormBuilder(); |
| 18 | + $formBuilder->add('favorite_fruit', ChoiceType::class, [ |
| 19 | + 'autocomplete' => true, |
| 20 | + 'label' => 'Your favorite fruit:', |
| 21 | + 'choices' => [ |
| 22 | + 'Apple' => 'apple', |
| 23 | + 'Banana' => 'banana', |
| 24 | + 'Cherry' => 'cherry', |
| 25 | + 'Coconut' => 'coconut', |
| 26 | + 'Grape' => 'grape', |
| 27 | + 'Kiwi' => 'kiwi', |
| 28 | + 'Lemon' => 'lemon', |
| 29 | + 'Mango' => 'mango', |
| 30 | + 'Orange' => 'orange', |
| 31 | + 'Papaya' => 'papaya', |
| 32 | + 'Peach' => 'peach', |
| 33 | + 'Pineapple' => 'pineapple', |
| 34 | + 'Pear' => 'pear', |
| 35 | + 'Pomegranate' => 'pomegranate', |
| 36 | + 'Pomelo' => 'pomelo', |
| 37 | + 'Raspberry' => 'raspberry', |
| 38 | + 'Strawberry' => 'strawberry', |
| 39 | + 'Watermelon' => 'watermelon', |
| 40 | + ], |
| 41 | + ]); |
16 | 42 |
|
17 | | - return $this->render( |
18 | | - 'ux_autocomplete/index.html.twig', |
19 | | - ['form' => $form->createView()] |
20 | | - ); |
| 43 | + $form = $formBuilder->getForm(); |
| 44 | + |
| 45 | + return $this->render('ux_autocomplete/without_ajax.html.twig', [ |
| 46 | + 'form' => $form->createView() |
| 47 | + ]); |
| 48 | + } |
| 49 | + |
| 50 | + #[Route('/with-ajax')] |
| 51 | + public function withAjax(): Response |
| 52 | + { |
| 53 | + $formBuilder = $this->createFormBuilder(); |
| 54 | + $formBuilder->add('favorite_fruit', FruitAutocompleteField::class); |
| 55 | + |
| 56 | + $form = $formBuilder->getForm(); |
| 57 | + |
| 58 | + return $this->render('ux_autocomplete/with_ajax.html.twig', [ |
| 59 | + 'form' => $form->createView() |
| 60 | + ]); |
21 | 61 | } |
22 | 62 |
|
23 | 63 | #[Route('/custom-controller')] |
24 | | - public function customController() |
| 64 | + public function customController(): Response |
25 | 65 | { |
26 | 66 | return $this->render('ux_autocomplete/custom_controller.html.twig'); |
27 | 67 | } |
|
0 commit comments