<?php
// your-path-to-types/ContactType.php
namespace myapplication\myBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array('attr' => array('placeholder' => 'Your name'),
'constraints' => array(
new NotBlank(array("message" => "Please provide your name")),
)
))
->add('subject', TextType::class, array('attr' => array('placeholder' => 'Subject'),
'constraints' => array(
new NotBlank(array("message" => "Please give a Subject")),
)
))
->add('email', EmailType::class, array('attr' => array('placeholder' => 'Your email address'),
'constraints' => array(
new NotBlank(array("message" => "Please provide a valid email")),
new Email(array("message" => "Your email doesn't seems to be valid")),
)
))
->add('message', TextareaType::class, array('attr' => array('placeholder' => 'Your message here'),
'constraints' => array(
new NotBlank(array("message" => "Please provide a message here")),
)
))
;
}
public function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'error_bubbling' => true
));
}
public function getName()
{
return 'contact_form';
}
}
<?php
namespace myapplication\myBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function contactAction(Request $request)
{
// Create the form according to the FormType created previously.
// And give the proper parameters
$form = $this->createForm('myapplication\myBundle\Form\ContactType',null,array(
// To set the action use $this->generateUrl('route_identifier')
'action' => $this->generateUrl('myapplication_contact'),
'method' => 'POST'
));
if ($request->isMethod('POST')) {
// Refill the fields in case the form is not valid.
$form->handleRequest($request);
if($form->isValid()){
// Send mail
if($this->sendEmail($form->getData())){
// Everything OK, redirect to wherever you want ! :
return $this->redirectToRoute('redirect_to_somewhere_now');
}else{
// An error ocurred, handle
var_dump("Errooooor :(");
}
}
}
return $this->render('myBundle:Default:contact.html.twig', array(
'form' => $form->createView()
));
}
private function sendEmail($data){
$myappContactMail = 'mycontactmail@mymail.com';
$myappContactPassword = 'yourmailpassword';
// In this case we'll use the ZOHO mail services.
// If your service is another, then read the following article to know which smpt code to use and which port
// http://ourcodeworld.com/articles/read/14/swiftmailer-send-mails-from-php-easily-and-effortlessly
$transport = \Swift_SmtpTransport::newInstance('smtp.zoho.com', 465,'ssl')
->setUsername($myappContactMail)
->setPassword($myappContactPassword);
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance("Our Code World Contact Form ". $data["subject"])
->setFrom(array($myappContactMail => "Message by ".$data["name"]))
->setTo(array(
$myappContactMail => $myappContactMail
))
->setBody($data["message"]."<br>ContactMail :".$data["email"]);
return $mailer->send($message);
}
}
在 symfony3 上
为联系表单创建 FormType
以下类包含稍后将用于在控制器中创建表单的 ContactType。
在 Twig 中创建视图
现在视图(在这种情况下,将通过 twig 呈现的视图)需要作为验证的基础:
创建控制器
现在,最重要的一点,将处理我们的表单的控制器。
像往常一样,您的控制器操作应该已经在 routing.yml 文件中有一个路径,并且它以它为目标:
最后,我们的控制器(带有联系动作)应该是这样的: