Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF][SF2][Symfony2]Request data
Forum PHP.pl > Forum > PHP > Frameworki
gentleman
witam,
  1. public function addAction(Request $request) {
  2.  
  3. $user = new Users();
  4. $form = $this->createFormBuilder($user)
  5. ->add('name', 'text')
  6. ->add('username', 'text')
  7. ->getForm();
  8.  
  9. $form->bindRequest($request);
  10.  
  11. if ($request->getMethod() == 'POST') {
  12.  
  13. $em = $this->getDoctrine()->getEntityManager();
  14. $em->persist($form->getData());
  15. $em->flush();
  16.  
  17. echo 'ok';
  18.  
  19. } else {
  20.  
  21. echo 'error';
  22.  
  23. }
  24.  
  25. }

dlaczego wyświetla mi się formularz z napisem error ?, na pewno metoda formularza jest post.

oraz chce wyświetlić np samo imię i żaden z tych niżej nie działa.
  1. $this->get('request')->request->get('name');
  2. $request->request->get('name');


coś źle robie? proszę o wskazówki i porady biggrin.gif

oczywiście mam dołączone
  1. use Symfony\Component\HttpFoundation\Request;
skowron-line
Co do formularza z napisem error to chyba proste tak ? Skoro nie jest spelniony warunek to wyświetla się wartość z else

A co do wyświetlania to masz w klasie request
  1. /**
  2.   * Gets a "parameter" value.
  3.   *
  4.   * This method is mainly useful for libraries that want to provide some flexibility.
  5.   *
  6.   * Order of precedence: GET, PATH, POST
  7.   *
  8.   * Avoid using this method in controllers:
  9.   *
  10.   * * slow
  11.   * * prefer to get from a "named" source
  12.   *
  13.   * It is better to explicitly get request parameters from the appropriate
  14.   * public property instead (query, attributes, request).
  15.   *
  16.   * @param string $key the key
  17.   * @param mixed $default the default value
  18.   * @param Boolean $deep is parameter deep in multidimensional array
  19.   *
  20.   * @return mixed
  21.   */
  22. public function get($key, $default = null, $deep = false)
  23. {
  24. return $this->query->get($key, $this->attributes->get($key, $this->request->get($key, $default, $deep), $deep), $deep);
  25. }

albo możesz sobie
  1. var_dump($request->request->all());
cadavre
  1. $form->bindRequest($request);

Masz w złym miejscu. Ta linijka ma się znaleźć za if'em a nie przed. : )

Jeśli dalej nie będzie banglać to strzelam, że masz błąd w Twigu.
ixox
Wyświetla "error", ponieważ pierwszy Request (przed wysłaniem formularza) nie jest metodą POST.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.