ins*_*ere 5 theming formbuilder symfony twig
也许我忽略了一些东西,希望这很容易.
我有一个表单,我最终想要的是以下结果:
领域:
应该a在标签和额外的div之后获得额外的-Tag,填写帮助和/或错误(如果适用).
我需要做的是,必填字段a使用以下方法获取-Tag:
{% use 'form_div_layout.html.twig' with field_label as base_field_label %}
{% block field_label %}
{{ block('base_field_label') }}
{% if required %}
<a href=""><span> </span></a>
{% endif %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
所以,我尝试过的是不同的版本:
{% use 'form_div_layout.html.twig' with field_label as base_field_label %}
{% block field_label %}
{{ block('base_field_label') }}
{% if required or help is defined %}
<a href=""><span> </span></a>
{% endif %}
{% endblock %}
{% block field_row %}
{% spaceless %}
<div class="row">
{% if required or help is defined %}
<div>
{{ form_errors(form) }}
{{ help }}
</div>
{% endif %}
{{ form_label(form) }}
{{ form_widget(form, { 'attr': {'class': 'grid_4'} }) }}
</div>
{% endspaceless %}
{% endblock field_row %}
Run Code Online (Sandbox Code Playgroud)
我无法让这个工作.
所以我的问题是:
我从哪里获得帮助文本,哪些文章也可以包含HTML?我在form builder没有成功的情况下尝试了这一点- 但至少有一个例外:
$builder ->add('subject', 'text', array(
'label' => 'Subject',
'help' => 'Can be formatted content with <strong>HTML-Elements</strong>',
));
Run Code Online (Sandbox Code Playgroud)如何判断当前字段是否有错误(向行添加类),如果是,还会显示它?{{ form_errors(form) }}没有输出任何东西,无论我把它放在`field_row˚内.
没有帮助文本,您必须为字段创建表单扩展并将其添加到默认选项.
SF 2.1 Beta 1中的示例:
namespace Webility\Bundle\WebilityBundle\Form\Extension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormViewInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class HelpFormTypeExtension extends AbstractTypeExtension
{
public function buildView(FormViewInterface $view, FormInterface $form, array $options){
$view->setVar('help', $options['help']);
}
public function getExtendedType(){
return 'field';
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'help' => null
));
}
}
Run Code Online (Sandbox Code Playgroud)
并将其注册为服务:
<service id="webility.form.extension.help" class="Webility\Bundle\WebilityBundle\Form\Extension\HelpFormTypeExtension">
<tag name="form.type_extension" alias="field" />
</service>
Run Code Online (Sandbox Code Playgroud)
对于错误问题:您是否有任何错误要打印?如果验证失败,请在控制器中检查:
echo '<pre>'; print_r( $form->getErrorsAsString() ); echo '</pre>'; exit;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2802 次 |
| 最近记录: |