Silex表单验证无需翻译

8
我想使用Silex的服务提供程序来构建一个简单的带有验证的联系表单,但似乎只能使用翻译服务提供程序。当我渲染视图时,会出现Twig_Error_Syntax错误:“过滤器“trans”不存在”,我猜测这是因为我必须自定义(覆盖)'form_div_layout.html.twig'并删除trans过滤器?我不需要翻译。
我还没有实现验证。
以下是我的代码:
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 ;
    }) ;

在联系页面中:

<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>
5个回答

24

我遇到了同样的问题,通过添加以下内容我成功解决了问题:

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

只是一个提示,我不得不把这个放在注册服务提供者的顶部,因为在doctrine / twig / form / validator / asset之后进行注册会导致问题。在创建您的服务提供者时,请将其放在顶部,一切都应该顺利进行。 - Jimbo

5
另一种方法是向Twig提供过滤器...
function dummy_trans($str) {
    return $str;
}

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

(注)星号表示动态的Twig过滤器,本质上是通配符。
我只进行了非常简短的测试,但似乎可以完成工作。

+1,因为这似乎是唯一独立于“Silex”的解决方案。可以通过使用匿名函数进一步改进,例如:$viewEnvironment->addFilter( 'trans*', new \Twig_Filter_Function( function($str) { return $str; } ) ); - Florian Wolters
这对于选择表单字段不起作用,即transChoice过滤器。Symfony的IdentityTranslator表明还需要做更多的工作来返回一组翻译的正确身份翻译。 - flu

2
Silex 文档 中指出:

如果您不想创建自己的表单布局,也没关系:将使用默认布局。但是,您必须注册翻译提供程序,因为默认表单布局需要它。

因此,如果您想使用默认布局,只需执行以下操作:
$app->register(new Silex\Provider\TranslationServiceProvider());

1
我通过以下方法成功避免了翻译错误:

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

1
解决方案是通过删除转换过滤器来自定义表单布局。

4
可以举个例子说明如何做到这一点吗? - Ben Waine
2
就目前而言,这个答案根本没有给出足够的解释。 - Jimbo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接