Skip to content

Instantly share code, notes, and snippets.

@michaelcullum
Created August 23, 2013 16:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save michaelcullum/6321332 to your computer and use it in GitHub Desktop.
Save michaelcullum/6321332 to your computer and use it in GitHub Desktop.
Contact Form for Silex
{% extends layout %}
{% block body %}
<div id="main">
<h2>Contact me</h2>
<p>For queries about anything on this website, or for job quotations, please contact me using the contact form below or via <a href="mailto:email@email.com">email</a>.</p>
<form action="#" method="post">
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_label(form.name, 'Your name:') }}<br />
<div class='form-input'>
{{ form_widget(form.name) }}
{{ form_errors(form.name) }}
</div>
</div>
<div>
{{ form_label(form.email, 'Your Email:') }}<br />
<div class='form-input'>
{{ form_errors(form.email) }}
{{ form_widget(form.email) }}
</div>
</div>
<div>
{{ form_label(form.message, 'Your Message:') }}<br />
<div class='form-textarea'>
{{ form_errors(form.message) }}
{{ form_widget(form.message) }}
</div>
</div>
<input type="submit" name="submit" />
{{ form_end(form) }}
</form>
</div>
{% endblock %}
<?php
namespace Michaelcullum\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints as Assert;
class BasicPages
{
public function contactAction(Request $request, Application $app)
{
$form = $app['form.factory']->createBuilder('form')
->add('name', 'text', array(
'required' => true,
'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 5)))
))
->add('email', 'text', array(
'required' => true,
'constraints' => array(new Assert\Email(), new Assert\NotBlank())
))
->add('message', 'textarea', array(
'required' => true,
'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 10)))
))
->getForm();
if ('POST' == $request->getMethod())
{
$form->bind($request);
if ($form->isValid())
{
$data = $form->getData();
//$contactEmail = 'enquiries@richardhutchinson.me.uk';
$contactEmail = 'm@michaelcullum.com';
$headers = 'From: ' . $contactEmail . "\r\n" .
'Reply-To: ' . $data['email'] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = sprintf('Richard,\r\n.
You were sent a message using the contact form on your website.\r\n
Message: %s.\r\n
It was sent from %s who can be contacted at %s.',
$data['message'], $data['name'], $data['email']);
mail($contactEmail, 'Contact Form', $data['message'], $headers);
return $app['twig']->render('contact.html.twig', array(
'page' => 'contact',
'title' => 'Page Title',
'submitted' => true,
));
}
}
return $app['twig']->render('contact.html.twig', array(
'page' => 'contact',
'title' => 'Richard Hutchinson: Contact me',
'form' => $form->createView(),
));
}
}
textarea {
width: 90%;
height: 100px;
border: 3px solid #cccccc;
padding: 5px;
font-family: Tahoma, sans-serif;
background-position: bottom right;
background-repeat: no-repeat;
}
input {
border: 3px solid #cccccc;
padding: 5px;
font-family: Tahoma, sans-serif;
background-position: bottom right;
background-repeat: no-repeat;
}
div.form-input {
padding: 5px;
}
div.form-textarea {
padding: 5px;
}
<?php
use Silex\Provider\FormServiceProvider;
use Symfony\Component\Validator\Constraints as Assert;
use Silex\Provider\ValidatorServiceProvider;
use Silex\Provider\TranslationServiceProvider;
$app->register(new FormServiceProvider());
$app->register(new ValidatorServiceProvider());
$app->register(new TranslationServiceProvider(), array(
'translator.messages' => array(),
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment