Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silex form validation without translation

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>
like image 596
EdUp Avatar asked Feb 28 '12 10:02

EdUp


3 Answers

Got the same problem and I was able to solve it by adding:

$app->register(new Silex\Provider\TranslationServiceProvider(), array(
    'translator.messages' => array(),
));
like image 92
RRikesh Avatar answered Sep 21 '22 00:09

RRikesh


Another way to do this would be to provide Twig with the filters...

function dummy_trans($str) {
    return $str;
}

$app['twig']->addFilter('trans*', new Twig_Filter_Function('dummy_trans'));

(N.B) the asterisk denotes a dynamic Twig filter, essentially a wildcard.

I've only tested this very briefly but seems to do the job.

like image 5
pete Avatar answered Sep 23 '22 00:09

pete


It is stated in the Silex documentation:

If you don't want to create your own form layout, it's fine: a default one will be used. But you will have to register the translation provider as the default form layout requires it.

So all you have to do, if you want to use the default layout, is the following:

$app->register(new Silex\Provider\TranslationServiceProvider());
like image 2
Jubstuff Avatar answered Sep 22 '22 00:09

Jubstuff