简体   繁体   中英

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>

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

$app->register(new Silex\Provider\TranslationServiceProvider(), array(
    'translator.messages' => array(),
));

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'));

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

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

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());

I was able to bypass the translation errors by doing this:

$app = new Silex\Application();
$app['translator.messages'] = array();

解决方案是通过删除跨过滤器来自定义表单布局

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM