简体   繁体   English

Silex表单验证没有翻译

[英]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 ? 我想使用Silex的服务提供商来构建一个简单的验证联系表单,但似乎只有翻译服务提供商,因为当我渲染视图时,我有一个Twig_Error_Syntax'过滤器“trans”不存在',我猜测是因为我必须自定义(覆盖)'form_div_layout.html.twig'并删除trans过滤器? 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... 另一种方法是为Twig提供过滤器......

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. (NB)星号表示动态Twig过滤器,基本上是通配符。

I've only tested this very briefly but seems to do the job. 我只是简单地测试了这个,但似乎做了这个工作。

It is stated in the Silex documentation : Silex文档中说明了这一点:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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