我正在Silex中构建一个表单,我想使用html5占位符属性,而不是只设置输入字段的预填充数据。
我正在尝试使用以下代码进行设置:
$form = $app['form.factory']->createBuilder('form')
->add('email','email', array(
'attr' => array('placeholder' => 'Email'),
'label' => 'Email'))
->add('password','password', array(
'attr' => array('placeholder' => 'Password'),
'label' => 'Password'))
->getForm();
但这似乎不起作用。 将以下代码放入我的twig模板后:
<form action="#" method="post">
{{ form_widget(form) }}
<input type="submit" name="submit" />
</form>
呈现以下HTML:
<form action="#" method="post">
<div id="form">
<div>
<label for="form_email" class="required"></label>
<input type="email" id="form_email" name="form[email]" required="required" placeholder="">
</div>
<div>
<label for="form_password" class="required"></label>
<input type="password" id="form_password" name="form[password]" required="required" placeholder="">
</div>
<input type="hidden" id="form__token" name="form[_token]" value="03398bf6d8670d8de7a3800d9b49c91d21affbc3">
</div>
<input type="submit" name="submit">
</form>
如何在Silex中实现占位符的使用?
编辑:来调用我的模板我使用以下代码:
return $app['twig']->render('index.twig',array(
'form'=> $form->createView()
));
答案 0 :(得分:1)
您的代码似乎对我很好。尝试运行下面的代码。将它放在一个新目录中,运行composer install
,然后运行php -S localhost:8000 app.php
以使用PHP开发服务器(如果你有PHP 5.4或更高版本)。这应该尽可能地复制我的设置。如果这样有效,我们就可以开始弄清楚它为什么不适合你了。
{
"require": {
"silex/silex": "~1.1",
"symfony/form": "~2.3",
"symfony/twig-bridge": "~2.3",
"twig/twig": ">=1.8,<2.0-dev",
"symfony/translation": "~2.3"
}
}
<?php
require_once __DIR__.'/vendor/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Silex\Provider\FormServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.templates' => array(
'form' => '<form action="#" method="post"> {{ form_widget(form) }} <input type="submit" name="submit" /> </form>'
)
));
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'translator.messages' => array(),
));
$app->get('/', function() use($app) {
$form = $app['form.factory']->createBuilder('form')
->add('email','email', array(
'attr' => array('placeholder' => 'Email'),
'label' => 'Email'))
->add('password','password', array(
'attr' => array('placeholder' => 'Password'),
'label' => 'Password'))
->getForm();
return $app['twig']->render('form', array('form'=> $form->createView()));
});
$app->run();
<form action="#" method="post">
<div id="form">
<div>
<label for="form_email" class="required">Email</label>
<input type="email" id="form_email" name="form[email]" required="required" placeholder="Email" />
</div>
<div>
<label for="form_password" class="required">Password</label>
<input type="password" id="form_password" name="form[password]" required="required" placeholder="Password" />
</div>
<input type="hidden" id="form__token" name="form[_token]" value="ba96e88afe7156803c70e41149a35d28edf80977" />
</div>
<input type="submit" name="submit" />
</form>
答案 1 :(得分:0)
我认为您必须手动构建表单:
<form action="#" method="post">
{{ form_errors(form) }}
{{ form_errors(form.email) }}
{{ form_label(form.email) }}
{{ form_widget(form.email, {'attr': {'placeholder': 'Email'} }) }}
{{ form_errors(form.password) }}
{{ form_label(form.password) }}
{{ form_widget(form.password, {'attr': {'placeholder': 'Password'} }) }}
{{ form_rest(form) }}
<input type="submit" name="submit" />
</form>