簡體   English   中英

Silex表單驗證沒有翻譯

[英]Silex form validation without translation

我想使用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>

遇到了同樣的問題,我可以通過添加:

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

另一種方法是為Twig提供過濾器......

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

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

(NB)星號表示動態Twig過濾器,基本上是通配符。

我只是簡單地測試了這個,但似乎做了這個工作。

Silex文檔中說明了這一點:

如果您不想創建自己的表單布局,那就沒關系:將使用默認表單布局。 但是您必須注冊翻譯提供程序,因為默認的表單布局需要它。

因此,如果要使用默認布局,您只需執行以下操作:

$app->register(new Silex\Provider\TranslationServiceProvider());

通過這樣做,我能夠繞過翻譯錯誤:

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

解決方案是通過刪除跨過濾器來自定義表單布局

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM