Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony][Symfony2]This form should not contain extra fields
bartos12
post
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 21.11.2011

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


Witam,

Każdy użytkownik na mojej stronie ma możliwość zmodyfikowania swoich danych, tj. imię, nazwisko, hasło. Chce, aby wszystko było zrobione za pomocą jednego formularza z tym, że jeżeli użytkownik nie chce zmienić hasła a jedynie imię i nazwisko to nie uaktywnia pola password (pole password normalnie jest disabled i można je odblokować za pomocą checkbox).

A teraz w czym problem. Mam taki formularz :

  1. $builder->add('userName', 'hidden', array("label" => 'Name'))
  2. ->add('userFirstname', 'text', array("label" => 'First name'))
  3. ->add('userLastname', 'text', array("label" => 'Last name'))
  4. ->add('', 'checkbox', array(
  5. "label" => "Change password",
  6. "value" => false
  7. ))
  8. ->add('userPass', 'repeated', array(
  9. 'type' => 'password',
  10. 'first_name' => 'Password',
  11. 'second_name' => 'Re-enter Password',
  12. 'invalid_message' => "The passwords don't match!",
  13. 'read_only' => true
  14. ))
  15. ->add('userId', 'hidden');


Jak widać już na samym początku blokuje pola password.
Akcja wygląda tak :

  1. $em = $this->getDoctrine()->getEntityManager();
  2.  
  3. if($request->getMethod() == "POST")
  4. {
  5. $user = new user();
  6. $form = $this->createForm(new UserConfigType(), $user);
  7. $form->bindRequest($request);
  8.  
  9. if($form->isValid())
  10. {
  11. $id = $user->getUserId();
  12. $object = $em->getRepository('AcmeFootballBundle:User')->find($id);
  13. $object->setUserFirstname($user->getUserFirstname());
  14. $object->setUserLastname($user->getUserLastname());
  15.  
  16. if($user->getPassword()){
  17. $encoder = $this->container->get('sha256salted_encoder');
  18. $password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
  19. $user->setUserPass($password);
  20. }
  21.  
  22. $em->flush();
  23. return $this->redirect ($this->generateUrl ('_userIndex'));
  24. }
  25. }else
  26. {
  27. $id = $this->get('security.context')->getToken()->getUser()->getUserId();
  28. $user = $em->getRepository('AcmeFootballBundle:User')->find($id);
  29. $form = $this->createForm(new UserConfigType(), $user);
  30. }
  31.  
  32. return $this->render('AcmeFootballBundle:User:config.html.twig', array(
  33. "form" => $form->createView()
  34. ));


i na koniec widok :

  1. <form action="{{ path('_userConfig') }}" method="post" {{ form_enctype(form) }}>
  2. {{form_widget(form)}}
  3. <input type="submit" class="button"/>
  4. </form>


Problem polega na tym że gdy próbuje zapisać zmiany w imieniu czy nazwisku bez aktywowania przez js pól password wszystko jest wporządku. Jednak gdy uaktywnie pole password to nie przechodze walidacji. Pojawia się komunikat "This form should not contain extra fields". Co najlepsze gdy pozbęde się

  1. 'read_only' => true


z formularza i spróbuje zapisać nowe hasło jest ok. Ale gdy z dezaktywuje a potem znów aktywuje pola znowu pojawia się ten błąd. Trochę to dziwne po przecież nie robię nic oprócz dodania atrybutu disabled.
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 20.12.2025 - 20:10