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:
<?php
namespace Blog\BackendBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Blog\BackendBundle\Entity\Email;
use Blog\BackendBundle\Form\EmailType;
use Blog\BackendBundle\Entity\SendEmail;
use Blog\BackendBundle\Form\SendEmailType;
/**
* SendEmail controller.
*
* @Route("/email")
*/
class SendEmailController extends Controller
{
/**
* Send email
*
* Creates a new Email entity.
*
* @Route("/send/newemails.html", name="mail_send_to_users")
* @Method("POST")
* @Template("BlogBackendBundle:Email:sendEmail.html.twig")
*/
public function sendAction(Request $request) {
$title = $_POST['blog_backendbundle_sendemail']['Title'];
$massage = $_POST['blog_backendbundle_sendemail']['textMail'];
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('BlogBackendBundle:Email')->findAll();
// getMail() -> get users emails
foreach($entities AS $e) {
$name = 'Radzio';
$message = \Swift_Message::newInstance()
->setSubject($title)
->setFrom('radoslawchojnacki@tlen.pl')
->setTo($e->getMail())
->setContentType("text/html")
->setBody($this->renderView('BlogBackendBundle:Email:email.html.twig', array('massage' => $massage))) ;
}
return $this->redirect($this->generateUrl('aftersend_email', array('param' => 1
)) );
}
/**
* Creates a form to create a Email send form.
*
* @param Email $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createSendForm()
{
$form = $this->createForm(new SendEmailType
(), null, array( 'action' => $this->generateUrl('mail_send_to_users'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Wyślij'));
return $form;
}
/**
* Send email site
*
* @Route("/send/mail.html", name="send_email")
* @Method("GET")
* @Template()
*/
public function sendEmailAction() {
$entity = new Email();
$form = $this->createSendForm();
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* After Send email site
*
* @Route("/aftersend/mail.html", name="aftersend_email")
* @Method("GET")
* @Template()
*/
public function aftersendEmailAction(Request $request) {
$param = $request->query->get('param');
'entity' => $param,
);
}
}
Klasa formularza:
<?php
namespace Blog\BackendBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
class SendEmailType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm
(FormBuilderInterface
$builder, array $options) {
$builder
->add('Title', null, array('required' => false, 'label' => 'Tytół maila: ' )) ->add('textMail', 'textarea', array('required' => false, 'label' => 'Treść maila: ')) ;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array( 'data_class' => 'Blog\BackendBundle\Entity\SendEmail'
));
}
public function getDefaultOptions
(array $options) {
$collectionConstraint = new Collection
(array( 'Title' => new NotBlank
(array('message' => 'Invalid email address')), // 'email' => new Email(array('message' => 'Invalid email address')),
));
$options['validation_constraint'] = $collectionConstraint;
}
/**
* @return string
*/
public function getName()
{
return 'blog_backendbundle_sendemail';
}
}
Entity:
<?php
namespace Blog\BackendBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints
as Assert;
class SendEmail {
/**
* @var string
*
* @Assert\NotBlank( message = "To pole nie może byc puste" )
*/
private $Title;
/**
* @var string
*
* @Assert\NotBlank( message = "To pole nie może byc puste" )
*/
private $textMail;
/**
* Set Title
*
* @param string $Title
* @return Title
*/
public function setTitle($Title)
{
$this->Title = $Title;
return $this;
}
/**
* Get Title
*
* @return string
*/
public function getTitle()
{
return $this->Title;
}
/**
* Set textMail
*
* @param string $textMail
* @return textMail
*/
public function setTextMail($textMail)
{
$this->textMail = $textMail;
return $this;
}
/**
* Get textMail
*
* @return string
*/
public function getTextMail()
{
return $this->textMail;
}
}
Widok:
{% extends '::layout.html.twig' %}
{% block contents %}
Email HTML
{{ form_start(form, {'attr': {'novalidate': 'novalidate'}} ) }}
<div>
{{ form_label(form.Title) }}
{{ form_errors(form.Title) }}
{{ form_widget(form.Title) }}
</div>
<div>
{{ form_label(form.textMail) }}
{{ form_errors(form.textMail) }}
{{ form_widget(form.textMail) }}
</div>
{{ form_end(form) }}
{% endblock %}