Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony2][Symfony] Walidacja formularza
Forum PHP.pl > Forum > PHP > Frameworki
radoslawchojnacki@tlen.pl
Chodzi o walidację formularza z którego dane nie są dodawane do bazy tylko wysyłane pocztą. Czyli normalnie nie mial klasy entity. Jednak popytałem się i poczytałem i dowiedziałem się , że klasa entity jest potrzebna do walidacji i że trzeba sobie ją stworzyć aby dokonać walidacji. Więc zrobiłem ale coś źle bo niedziała. poniżej przedstawiam kod:

Kontroler:
  1. <?php
  2.  
  3. namespace Blog\BackendBundle\Controller;
  4.  
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Blog\BackendBundle\Entity\Email;
  11. use Blog\BackendBundle\Form\EmailType;
  12. use Blog\BackendBundle\Entity\SendEmail;
  13. use Blog\BackendBundle\Form\SendEmailType;
  14.  
  15. /**
  16.  * SendEmail controller.
  17.  *
  18.  * @Route("/email")
  19.  */
  20. class SendEmailController extends Controller
  21. {
  22. /**
  23.   * Send email
  24.   *
  25.   * Creates a new Email entity.
  26.   *
  27.   * @Route("/send/newemails.html", name="mail_send_to_users")
  28.   * @Method("POST")
  29.   * @Template("BlogBackendBundle:Email:sendEmail.html.twig")
  30.   */
  31. public function sendAction(Request $request) {
  32.  
  33. $title = $_POST['blog_backendbundle_sendemail']['Title'];
  34. $massage = $_POST['blog_backendbundle_sendemail']['textMail'];
  35.  
  36. $em = $this->getDoctrine()->getManager();
  37. $entities = $em->getRepository('BlogBackendBundle:Email')->findAll();
  38. // getMail() -> get users emails
  39. $mails = array();
  40. foreach($entities AS $e) {
  41. $name = 'Radzio';
  42. $message = \Swift_Message::newInstance()
  43. ->setSubject($title)
  44. ->setFrom('radoslawchojnacki@tlen.pl')
  45. ->setTo($e->getMail())
  46. ->setContentType("text/html")
  47. ->setBody($this->renderView('BlogBackendBundle:Email:email.html.twig', array('massage' => $massage)))
  48. ;
  49. }
  50. return $this->redirect($this->generateUrl('aftersend_email', array('param' => 1 )) );
  51.  
  52. }
  53.  
  54.  
  55. /**
  56.   * Creates a form to create a Email send form.
  57.   *
  58.   * @param Email $entity The entity
  59.   *
  60.   * @return \Symfony\Component\Form\Form The form
  61.   */
  62. private function createSendForm()
  63. {
  64. $form = $this->createForm(new SendEmailType(), null, array(
  65. 'action' => $this->generateUrl('mail_send_to_users'),
  66. 'method' => 'POST',
  67. ));
  68.  
  69. $form->add('submit', 'submit', array('label' => 'Wyślij'));
  70.  
  71. return $form;
  72. }
  73.  
  74. /**
  75.   * Send email site
  76.   *
  77.   * @Route("/send/mail.html", name="send_email")
  78.   * @Method("GET")
  79.   * @Template()
  80.   */
  81. public function sendEmailAction() {
  82.  
  83. $entity = new Email();
  84. $form = $this->createSendForm();
  85.  
  86. return array(
  87. 'entity' => $entity,
  88. 'form' => $form->createView(),
  89. );
  90. }
  91.  
  92. /**
  93.   * After Send email site
  94.   *
  95.   * @Route("/aftersend/mail.html", name="aftersend_email")
  96.   * @Method("GET")
  97.   * @Template()
  98.   */
  99. public function aftersendEmailAction(Request $request) {
  100.  
  101. $param = $request->query->get('param');
  102. return array(
  103. 'entity' => $param,
  104. );
  105. }
  106. }


Klasa formularza:
  1. <?php
  2.  
  3. namespace Blog\BackendBundle\Form;
  4.  
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  8. use Symfony\Component\Validator\Constraints\NotBlank;
  9.  
  10. class SendEmailType extends AbstractType
  11. {
  12. /**
  13.   * @param FormBuilderInterface $builder
  14.   * @param array $options
  15.   */
  16. public function buildForm(FormBuilderInterface $builder, array $options)
  17. {
  18. $builder
  19. ->add('Title', null, array('required' => false, 'label' => 'Tytół maila: ' ))
  20. ->add('textMail', 'textarea', array('required' => false, 'label' => 'Treść maila: '))
  21. ;
  22. }
  23.  
  24. /**
  25.   * @param OptionsResolverInterface $resolver
  26.   */
  27. public function setDefaultOptions(OptionsResolverInterface $resolver)
  28. {
  29. $resolver->setDefaults(array(
  30. 'data_class' => 'Blog\BackendBundle\Entity\SendEmail'
  31. ));
  32. }
  33.  
  34. public function getDefaultOptions(array $options)
  35. {
  36. $collectionConstraint = new Collection(array(
  37. 'Title' => new NotBlank(array('message' => 'Invalid email address')),
  38. // 'email' => new Email(array('message' => 'Invalid email address')),
  39. ));
  40.  
  41. $options['validation_constraint'] = $collectionConstraint;
  42. }
  43.  
  44. /**
  45.   * @return string
  46.   */
  47. public function getName()
  48. {
  49. return 'blog_backendbundle_sendemail';
  50. }
  51. }
  52.  

Entity:
  1. <?php
  2.  
  3. namespace Blog\BackendBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use Gedmo\Timestampable\Traits\TimestampableEntity;
  8. use Symfony\Component\Validator\Validation;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10.  
  11. class SendEmail {
  12.  
  13. /**
  14.   * @var string
  15.   *
  16.   * @Assert\NotBlank( message = "To pole nie może byc puste" )
  17.   */
  18. private $Title;
  19.  
  20. /**
  21.   * @var string
  22.   *
  23.   * @Assert\NotBlank( message = "To pole nie może byc puste" )
  24.   */
  25. private $textMail;
  26.  
  27. /**
  28.   * Set Title
  29.   *
  30.   * @param string $Title
  31.   * @return Title
  32.   */
  33. public function setTitle($Title)
  34. {
  35. $this->Title = $Title;
  36.  
  37. return $this;
  38. }
  39.  
  40. /**
  41.   * Get Title
  42.   *
  43.   * @return string
  44.   */
  45. public function getTitle()
  46. {
  47. return $this->Title;
  48. }
  49.  
  50. /**
  51.   * Set textMail
  52.   *
  53.   * @param string $textMail
  54.   * @return textMail
  55.   */
  56. public function setTextMail($textMail)
  57. {
  58. $this->textMail = $textMail;
  59.  
  60. return $this;
  61. }
  62.  
  63. /**
  64.   * Get textMail
  65.   *
  66.   * @return string
  67.   */
  68. public function getTextMail()
  69. {
  70. return $this->textMail;
  71. }
  72.  
  73. }


Widok:

  1. {% extends '::layout.html.twig' %}
  2.  
  3. {% block contents %}
  4. Email HTML
  5.  
  6. {{ form_start(form, {'attr': {'novalidate': 'novalidate'}} ) }}
  7. <div>
  8. {{ form_label(form.Title) }}
  9. {{ form_errors(form.Title) }}
  10. {{ form_widget(form.Title) }}
  11. </div>
  12. <div>
  13. {{ form_label(form.textMail) }}
  14. {{ form_errors(form.textMail) }}
  15. {{ form_widget(form.textMail) }}
  16. </div>
  17. {{ form_end(form) }}
  18.  
  19. {% endblock %}


404
Gdzie czytałeś, że trzeba tworzyć entity? Spróbuj stworzyć ten formularz bez entity na podstawie tego: http://symfony.com/doc/current/book/forms....without-a-class.
radoslawchojnacki@tlen.pl
Nie no, sposób jest dobry. nie pamiętam już gdzie to czytałem.
Błąd był banalny facepalmxd.gif

wystarczy dodać w sendAction

  1. if ($form->isValid()) {
  2. ...
  3. ...
  4. }


Choć z tego co widzę to enity jednak chyba potrzebne nie jest.
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.