I'd like to use Silex's service providers just to build a simple contact form with validation but it seems to be only with translation service provider because when I render the view I have a Twig_Error_Syntax 'The filter "trans" does not exist', I guess it's because I have to customize(override) 'form_div_layout.html.twig' and remove trans filter ? I don't need translation.
I didn't implement validation yet.
Here's my code :
use Symfony\Component\HttpFoundation\Request ;
use Symfony\Component\HttpFoundation\Response ;
require_once __DIR__ . '/bootstrap.php' ;
$app = new Silex\Application() ;
require __DIR__ . '/../config/conf.php';
$app->register(new Silex\Provider\SymfonyBridgesServiceProvider(), array(
'symfony_bridges.class_path' => __DIR__ . '/../vendor/symfony/src',
)) ;
$app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
'http_cache.cache_dir' => __DIR__ . '/../cache/',
)) ;
$app->register(new Silex\Provider\FormServiceProvider(), array(
'form.class_path' => __DIR__ . '/../vendor/symfony/src'
)) ;
$app->register(new Silex\Provider\ValidatorServiceProvider(), array(
'validator.class_path' => __DIR__ . '/../vendor/symfony/src',
)) ;
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/../src/views/frontend/',
'twig.class_path' => __DIR__ . '/../vendor/twig/lib',
'twig.options' => array('cache' => $app['http_cache.cache_dir'] . 'twig.cache'),
)) ;
$app->get('/contact', function (Silex\Application $app) use ($navigation) {
$form = $app['form.factory']->createBuilder('form')
->add('name', 'text')
->add('surname', 'text')
->add('email', 'email')
->add('message', 'textarea')
->getForm() ;
$response = new Response() ;
$page = $app['twig']->render('contact.html.twig', array('navigation' => $navigation, 'form' => $form->createView())) ;
$response->setContent($page) ;
return $response ;
}) ;
and in the contact page :
<form class="form-horizontal" action="/contact" method="post">
<fieldset class="control-group">
<legend>Contact</legend>
{{ form_errors(form) }}
{{ form_row(form.name) }
{{ form_row(form.surname) }}
{{ form_row(form.email) }}
{{ form_row(form.message) }}
<button type="submit" class="btn btn-info">Send</button>
</fieldset>
</form>