Advertisement
djuro95

Form

Feb 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. Form
  2.  
  3. use Symfony\Component\Form\Extension\Core\Type\TextType;
  4. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  5.  
  6. $form = $this->createFormBuilder()
  7. ->setMethod('GET')
  8. ->add('search', TextType::class)
  9. //->add('submit', SubmitType::class) //nije preporucljivo
  10. ->getForm();
  11.  
  12. return $this->render('CarBundle:Default:index.html.twig',
  13. [
  14. 'cars' => $cars,
  15. 'form' => $form->createView()
  16. ]
  17. );
  18.  
  19.  
  20. **************
  21.  
  22. form-a u view
  23.  
  24. {{ form_start(form, { attr: {class: 'form-inline'} }) }}
  25. {{ form_widget(form.search, { attr: {placeholder: 'Search'} }) }}
  26. <button type="submit" class="btn">Search</button>
  27. {{ form_end(form) }}
  28.  
  29. ------------------------------
  30. in DafaultController
  31.  
  32. public function indexAction(Request $request)
  33. {
  34. $carRepository = $this->getDoctrine()->getRepository('Car');
  35. $cars = $carRepository->findCarsWithDetails();
  36. $form = $this->createFormBuilder()
  37. ->setMethod('GET')
  38. ->add('search', TextType::class,[
  39. 'constraints' => [
  40. new NotBlank(),
  41. new Length(['min' => 2])
  42. ]
  43. ])
  44. ->getForm();
  45.  
  46. $form->handleRequest($request);
  47.  
  48. if($form->isSubmitted() && $form->isValid()){
  49. die('Form submitted')l
  50. }
  51.  
  52. return $this->render('CarBundle:Default:index.html.twig',
  53. [
  54. 'cars' => $cars,
  55. 'form' => $form->createView()
  56. ]
  57. );
  58. }
  59.  
  60. **********************
  61. change on View
  62.  
  63. {{ form_errors(form.search) }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement