我正在使用表单构建器构建表单,我需要在某些字段后添加<hr/>
。我该怎么做?仅供参考,需要在字段priority
后添加<hr/>
,在biography
之前添加另一个。我的表单生成器:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text',
array(
'label' => 'Name English',
))
->add('bangla_name','text',
array(
'label' => 'Name Bangla',
'required' => false
))
->add('real_name','text',
array(
'required' => false
))
->add('debut', 'text',array(
'label' => 'Debut Film',
'required' => false))
->add('birth_place','text',array(
'label'=> 'Birth Place',
'required' => false
))
->add('sex', 'choice', array(
'choices' => array('M' => 'Male', 'F' => 'Female', 'N/A' => 'N/A'),
'row_attr' => array(
'class' => 'col-sm-6 n-p-l'),
'label'=> 'Sex',
'label_attr' => array(
'class' => 'col-md-4'
),
))
->add('priority','choice', array('required' => true, 'label'=> 'Priority','attr'=> array('class'=>'col-md-2'), 'choices' => array(
'0' => '0',
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4'
), 'row_attr' => array(
'class' => 'col-sm-4'
),
'label_attr' => array(
'class' => 'col-md-6'
),
))
->add('bday','choice',
array(
'required' => true, 'choices' => $this->buildDayChoices(),'label'=> 'Birth Day',
'row_attr' => array(
'class' => 'col-sm-4 n-p-l'),
'help' => '*required',
'label_attr' => array(
'class' => 'col-md-6'
)
))
->add('bmonth','choice',
array(
'required' => true, 'choices' => $this->buildMonthChoices(),'label'=> 'Birth Month',
'row_attr' => array(
'class' => 'col-sm-4 n-p-l'),
'help' => '*required',
'label_attr' => array(
'class' => 'col-md-6'
),
))
->add('byear','choice',
array(
'required' => true, 'choices' => $this->buildYearChoices(),'label'=> 'Birth Year',
'row_attr' => array(
'class' => 'col-sm-4 n-p-l'),
'help' => '*required',
'label_attr' => array(
'class' => 'col-md-6'
),
))
->add('dod_day','choice',
array(
'required' => true, 'choices' => $this->buildDayChoices(),'label'=> 'Death Day',
'row_attr' => array(
'class' => 'col-sm-4 n-p-l'),
'help' => '*required',
'label_attr' => array(
'class' => 'col-md-6'
),
))
->add('dod_month','choice',
array(
'required' => true, 'choices' => $this->buildDayChoices(),'label'=> 'Death Month',
'row_attr' => array(
'class' => 'col-sm-4 n-p-l'),
'help' => '*required',
'label_attr' => array(
'class' => 'col-md-6'
),
))
->add('dod_year','choice',
array(
'required' => true, 'choices' => $this->buildDayChoices(),'label'=> 'Death Year',
'row_attr' => array(
'class' => 'col-sm-4 n-p-l'),
'help' => '*required',
'label_attr' => array(
'class' => 'col-md-6'
),
))
// ->add('birth','birthday', array('label' => 'Date of Birth', 'years' => range(1950, date('Y')-10)))
->add('bio_english','textarea',array(
'label'=> 'Biography - English',
))
->add('bio_bangla','textarea',array(
'label'=> 'Biography - Bangla',
))
->add('graphics', 'graphics', array(
'upload_directory' => 'uploads' . DIRECTORY_SEPARATOR . 'artist','row_attr' => array(
'class' => 'col-sm-12')
))
;
}
我认为你不能。水平规则不是表单构造,因此,表单构建器没有定义任何业务。
这需要在视图/模板中处理
{{ form_row(form.priority) }}
<hr/>
{{ form_row(form.bday) }}
IMO,您在构建器中使用HTML属性时有点过于自由。仅当您确实需要它们时,才应使用这些。同样,它们主要是观点问题。
编辑
您可以在窗体视图中添加此自定义项,并覆盖form_row
块以了解此自定义项
src/AppBundle/Form/YourFormType.php
<?php
namespace AppBundle'Form;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
use Symfony'Component'OptionsResolver'OptionsResolverInterface;
use Symfony'Component'Form'FormView;
use Symfony'Component'Form'FormInterface;
class YourFormType extends AbstractType
{
/* ... Rest of form type ... */
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view['priority']->use_separator = true;
parent::buildView($view, $form, $options);
}
}
然后形成块
{% block form_row %}
{{ parent() }}
{% if form.use_separator is defined %}
<hr/>
{% endif %}
{% endblock %}
好吧,正如我在评论中所说,一种方法是覆盖表单的特定字段。如果您对该主题感兴趣,可以在此处阅读更多内容。
为了演示,我创建了包含 3 个字段的简单表单,如下所示:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('username', 'text');
$builder->add('email', 'email');
$builder->add('password', 'password');
}
现在,这里重要的部分是表单的名称(稍后在覆盖时使用)
public function getName() {
return 'myform';
}
这里没有什么新鲜事,大部分魔术都发生在您的模板文件中。
{% form_theme form _self %}
以下行告诉Symfony您正在使用相同的模板文件作为创建表单主题的基础。下一个:
{% block _myform_email_row %}
{{ block('form_row') }}
<hr/>
{% endblock %}
在{% form_theme ... %}
之后,您需要在模板顶部定义此块
{% block _myform_email_row %}
- 显然,
_myform
是您表单的名称。 -
_email
是要覆盖的输入的名称 -
_row
是您要覆盖其中的哪一部分。row
代表标签、小部件和块。_widget
将仅覆盖呈现输入的部分。
这就是你所需要的。定义自定义块,渲染默认块并在其末尾添加<hr/>
。您可以像这样覆盖任何模板部件。
编辑 - 对评论的回复
但不能完全使用它,因为我不能使用 {% form_theme形式_self %}
您可以删除整个{% form_theme %}
行,并将自定义{% block _myform_... %}
移动到单独的模板。然后打开 config.yml,查找twig
部分并将模板作为资源包含在内,如下所示:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form:
resources:
- theme/custom.html.twig
在这里,我的模板位于应用程序/资源/视图/主题中
这将包括您拥有的每个表单中的模板。但是,由于您覆盖了特定表单的特定字段,因此它不应与其他表单交互。这种方式将为您省去使用{% form_theme %}
块的麻烦。