Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SF][SF2][Symfony2]Request data
gentleman
post
Post #1





Grupa: Zarejestrowani
Postów: 41
Pomógł: 1
Dołączył: 17.05.2013

Ostrzeżenie: (0%)
-----


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 (IMG:style_emoticons/default/biggrin.gif)

oczywiście mam dołączone
  1. use Symfony\Component\HttpFoundation\Request;


Ten post edytował gentleman 31.05.2013, 15:08:06
Go to the top of the page
+Quote Post
skowron-line
post
Post #2





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

Ostrzeżenie: (0%)
-----


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());
Go to the top of the page
+Quote Post
cadavre
post
Post #3





Grupa: Zarejestrowani
Postów: 472
Pomógł: 7
Dołączył: 7.12.2005
Skąd: Gliwice

Ostrzeżenie: (0%)
-----


  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.

Ten post edytował cadavre 3.06.2013, 21:04:04
Go to the top of the page
+Quote Post
ixox
post
Post #4





Grupa: Zarejestrowani
Postów: 51
Pomógł: 13
Dołączył: 8.09.2012

Ostrzeżenie: (0%)
-----


Wyświetla "error", ponieważ pierwszy Request (przed wysłaniem formularza) nie jest metodą POST.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 14:55