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