Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony][SF2] Problem z walidacją daty
ssstrz
post 18.03.2014, 18:24:23
Post #1





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


Staram się dodać pole z datą do encji. Mój formularz:
  1. class ProgressType extends AbstractType
  2. {
  3. /**
  4.   * @param FormBuilderInterface $builder
  5.   * @param array $options
  6.   */
  7. public function buildForm(FormBuilderInterface $builder, array $options)
  8. {
  9. $builder
  10. ->add('title')
  11. ->add('description')
  12. ->add('started', 'datetime')
  13. ->add('ended', 'datetime')
  14. ;
  15. }
  16.  
  17. /**
  18.   * @param OptionsResolverInterface $resolver
  19.   */
  20. public function setDefaultOptions(OptionsResolverInterface $resolver)
  21. {
  22.  
  23. $resolver->setDefaults(array(
  24. 'csrf_protection' => false,
  25. 'csrf_field_name' => '_dc',
  26. 'data_class' => 'CodeCats\PanelBundle\Entity\Progress'
  27. ));
  28. }
  29.  
  30. /**
  31.   * @return string
  32.   */
  33. public function getName()
  34. {
  35. return 'codecats_panelbundle_progress';
  36. }
  37. }


w encji pola daty i czasu opisane są:
  1. /**
  2.   * @var DateTime
  3.   *
  4.   * @ORM\Column(name="started", type="datetime")
  5.   */
  6. private $started;
  7.  
  8. /**
  9.   * @var DateTime
  10.   *
  11.   * @ORM\Column(name="ended", type="datetime")
  12.   */
  13. private $ended;


Akcja w kontrolerze:

  1. $progress = new Progress();
  2. $form = $this->createForm(new ProgressType(), $progress);
  3.  
  4. $store = array(
  5. 'title' => 'aa',
  6. 'ended' => new DateTime()
  7. );
  8. $form->submit($store);
  9.  
  10. if ($form->isValid()) {
  11. //$progress->setStarted(new DateTime());
  12. // $em->flush();
  13.  
  14. return new JsonResponse(array('success' => true));
  15. }


Jednak walidacja nie przechodzi ponieważ:
  1. 'ended' =>
  2. 0 => 'This value is not valid.',
  3. 'date' =>
  4. 'year' =>
  5. ),
  6. 'month' =>
  7. ),
  8. 'day' =>
  9. ),
  10. ),
  11. 'time' =>
  12. 'hour' =>
  13. ),
  14. 'minute' =>
  15. ),
  16. ),
  17. ),
  18. )

Jak zasymulować wprowadzene daty tak aby walidacja działała poprawnie?
Go to the top of the page
+Quote Post
toffiak
post 19.03.2014, 08:06:34
Post #2





Grupa: Zarejestrowani
Postów: 395
Pomógł: 80
Dołączył: 24.08.2009

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


Pokaż reguły walidacji encji.


--------------------
Go to the top of the page
+Quote Post
ssstrz
post 10.04.2014, 20:57:15
Post #3





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


Nie dodawałem walidacji do encji.

Odświeżam temat, z tego co udało mi się dowiedzieć powinienem użyć Data Transformers http://symfony.com/doc/current/cookbook/fo...ansformers.html
Nie wiem co robię źle, dane dane pochodzące z PUT:
  1. {"id":5,"title":"aa","description":"44","started":"2014-03-18"}


  1. class StringToDateTimeTransformer implements DataTransformerInterface{
  2.  
  3. public function transform($value)
  4. {
  5.  
  6. if ( ! $value) {
  7. return null;
  8. }
  9.  
  10. $string = date_parse(date_format($value, "Y-m-d H:i:s"));
  11. if ( ! $string) {
  12. throw new TransformationFailedException(sprintf(
  13. 'Cant transform date to string!',
  14. $value
  15. ));
  16. }
  17.  
  18. return $string;
  19. }
  20.  
  21. public function reverseTransform($value)
  22. {
  23. if ( ! $value) {
  24. return new \DateTime("now");
  25. }
  26. $date = new \DateTime((string)$value["started"]);
  27.  
  28. return $date;
  29. }
  30. }


  1. $builder
  2. ->add('title')
  3. ->add('description')
  4. ->add(
  5. $builder->create('started', 'text')->addViewTransformer(new StringToDateTimeTransformer())
  6. )
  7. ;


akcja w kontrolerze:
  1. public function updateAction(Request $request, $id)
  2. {
  3. $fb = $this->get('fire_php');
  4.  
  5. $em = $this->getDoctrine()->getManager();
  6.  
  7. $progress = $em->getRepository('CodeCatsPanelBundle:Progress')->find($id);
  8. $form = $this->createForm(new ProgressType(), $progress);
  9.  
  10. $form->handleRequest($request);
  11.  
  12. if ($form->isValid()) {
  13. $em->flush();
  14.  
  15. return new JsonResponse(array('success' => true));
  16. }
  17. $fb->log($this->getErrorMessages($form));
  18.  
  19.  
  20. return new JsonResponse(array('success' => false, 'errors' => $this->getErrorMessages($form)));
  21. }
Go to the top of the page
+Quote Post
pyro
post 10.04.2014, 21:29:39
Post #4





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

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


Masz taki typ formularza jak datetime i tam masz nawet możliwośc wyboru widgeta jakiego użyć, łącznie z jego formatem. Nie ma potrzeby używać transformerów.

// EDIT

A Twój kod nie działa, bo używasz view transformera i starasz się zrobić ze stringa obiekt (dlatego pokazuje że nieprawidłowy typ), powinieneś użyć model transformera.

Ten post edytował pyro 10.04.2014, 21:48:38


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
ssstrz
post 11.04.2014, 11:28:38
Post #5





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


Tylko że ja nie mam wpływ na wygląd formularza (nie mam wpływu na frontend bo to robi ktoś inny przy pomocy frameworka do JS) Dane otrzymuję w postaci JSON i chcę zapisać je w bazie danych, mogę uzupełniać każde pole osobno ale chciałbym robić tak jak się powinno robić czyli na wzór $form->handleRequest, jak to wykonać w sf2?
Go to the top of the page
+Quote Post
pyro
post 11.04.2014, 12:27:43
Post #6





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

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


A jak to się odnosi do mojej odpowiedzi? Przecież ja Ci nie mówię, żeby użyć choćby skrawka html/css/js. Przeczytaj jeszcze raz.


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
ssstrz
post 11.04.2014, 12:48:47
Post #7





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


Chodzi o to?

  1. $builder
  2. ->add('title')
  3. ->add('description')
  4. ->add('started', 'datetime')
  5. ;
Go to the top of the page
+Quote Post
pyro
post 11.04.2014, 12:57:20
Post #8





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

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


Tak


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
ssstrz
post 11.04.2014, 13:29:02
Post #9





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


to teraz wrócilismy do 1 posta z tematu z tą różnicą że zamiast $form->submit używam $form->handleRequest .... validacja false

  1. public function updateAction(Request $request, $id)
  2. {
  3. $fb = $this->get('fire_php');
  4.  
  5. $em = $this->getDoctrine()->getManager();
  6.  
  7. $progress = $em->getRepository('CodeCatsPanelBundle:Progress')->find($id);
  8. $fb->log($progress);
  9. $form = $this->createForm(new ProgressType(), $progress);
  10.  
  11. $form->handleRequest($request);
  12.  
  13. if ($form->isValid()) {
  14. $em->flush();
  15.  
  16. return new JsonResponse(array('success' => true));
  17. }
  18.  
  19. return new JsonResponse(array('success' => false, 'errors' => $this->getErrorMessages($form)));
  20. }
Go to the top of the page
+Quote Post
pyro
post 11.04.2014, 13:32:30
Post #10





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

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


Niech zgadnę... zostawiłeś transformera?


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
ssstrz
post 11.04.2014, 15:16:01
Post #11





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


Cytat(pyro @ 11.04.2014, 14:32:30 ) *
Niech zgadnę... zostawiłeś transformera?

tak... zakomentowanego.
  1. $builder
  2. ->add('title')
  3. ->add('description')
  4. // ->add('id')
  5. ->add('started', 'date')//datetime też sprawdzałem
  6. // ->add('ended', 'datetime')
  7. // ->add('started', 'text')
  8. // ->addViewTransformer(new StringToDateTimeTransformer())
  9. // ->add(
  10. // $builder->create('started', 'text')->addModelTransformer(new StringToDateTimeTransformer())
  11. // )
  12. ;


Ten post edytował ssstrz 11.04.2014, 15:16:57
Go to the top of the page
+Quote Post
pyro
post 11.04.2014, 15:18:16
Post #12





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

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


Ok a jak renderujesz to pole i jakie dane wprowadzasz?


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
ssstrz
post 11.04.2014, 15:57:10
Post #13





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


Pisałem że dane otrzymuję z frameworka JS (extJS) więc ja tego nie renderuje - w assetsach mam pliki js które renderują formularz ale wypływ na formatowanie pola mam niewielki. Dostęp mam do danych np z firebuga metoda Put:
Kod
Source
{"id":5,"title":"aa","description":"aa","started":"2014-03-18"}


Go to the top of the page
+Quote Post
pyro
post 11.04.2014, 16:13:17
Post #14





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

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


Aha... to wyrenederuj u siebie testowo i porównaj z polami z layoutu. To, że pole ma name="abc", nie znaczy, że będzie $form->add('abc'...)


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
ssstrz
post 11.04.2014, 23:10:32
Post #15





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


Wyrenderowany formularz ma pola typu
  1. name="title"
i walidacja w ten sposób przechodz przechodzi. Po zmianie metody na put teść błędu walidacji to:
Kod
This form should not contain extra fields.
, Sf2 zgodnie z cookbook dodaje ukryte pole _method, i teraz walidacja (po wysłaniu formularza po wygenerowaniu) nie przechodzi z powodu w/w błędu

Ten post edytował ssstrz 11.04.2014, 23:12:05
Go to the top of the page
+Quote Post
skowron-line
post 12.04.2014, 17:33:26
Post #16





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

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


Jeżeli dodajesz extra pole to musisz dać
  1. 'mapped'=> false
  2. )

w ProcessType


--------------------
I'm so fast that last night I turned off the light switch in my hotel room and was in bed before the room was dark - Muhammad Ali.
Peg jeżeli chcesz uprawiać sex to dzieci muszą wyjść, a jeżeli chcesz żeby był dobry ty też musisz wyjść - Al Bundy.

QueryBuilder, Mootools.net, bbcradio1::MistaJam
http://www.phpbench.com/
Go to the top of the page
+Quote Post
ssstrz
post 13.04.2014, 20:38:50
Post #17





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


ok było kilka błedów po drodze, ale rozwiązanie problemu sprowadza się do zmapowania pól z formularza ExtJS do pól formularza Symfony 2 czyli chciałbym aby pole:
nadesłane z ExtJS
Kod
title


formularz bindował jako:
Kod
progress[title]
Go to the top of the page
+Quote Post
skowron-line
post 13.04.2014, 21:30:10
Post #18





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

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


W Propelu masz metode fromArray
  1. $sample->fromArray(array(
  2. 'Name' => 'skowron-line'
  3. )); // tu oczywiści podstawisz sobie dane które Ci przylecą

wiec przekazując obiekt do formularza masz dane.
  1. $this->createForm(new SampleType(), $sample);


--------------------
I'm so fast that last night I turned off the light switch in my hotel room and was in bed before the room was dark - Muhammad Ali.
Peg jeżeli chcesz uprawiać sex to dzieci muszą wyjść, a jeżeli chcesz żeby był dobry ty też musisz wyjść - Al Bundy.

QueryBuilder, Mootools.net, bbcradio1::MistaJam
http://www.phpbench.com/
Go to the top of the page
+Quote Post
ssstrz
post 17.04.2014, 04:50:08
Post #19





Grupa: Zarejestrowani
Postów: 103
Pomógł: 17
Dołączył: 15.12.2012

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


a czy jest sposób aby form builder automatycznie mapował pole title na progress[title] czy muszę odbierać te parametry ręcznie?

.... update

wygląda na to że createNamed to rozwiązenie mojego problemu.

  1. $factory = Forms::createFormFactory();
  2.  
  3. $progress = $em->getRepository('CodeCatsPanelBundle:Progress')->find($id);
  4.  
  5. $form = $factory->createNamed(null, new ProgressType(), $progress, array('method'=>'PUT'));
  6. $form->submit(json_decode($request->getContent(), true));
  7.  
  8. if ($form->isValid()) {
  9. $em->flush();
  10.  
  11. return new JsonResponse(array('success' => true));
  12. }
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 27.04.2024 - 11:36