如何在 Symfony2 表单生成器中添加 <hr>?
How to add a <hr> in Symfony2 form builder?
我正在使用表单生成器构建表单,我需要在某些字段后添加 <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')
))
;
}
我认为你做不到。水平线不是表单构造,因此,没有由 FormBuilder 定义的业务。
这需要在view/template
中处理
{{ form_row(form.priority) }}
<hr/>
{{ form_row(form.bday) }}
IMO,您在构建器中使用 HTML 属性有点过于自由了。只有当你真的需要它们时才应该使用它们。同样,它们主要是视图问题。
编辑
您可以在表单的视图中添加此自定义,并且override the form_row
block了解此自定义
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 %}
好吧,正如我在评论中所说,一种方法是覆盖表单的特定字段。如果您对该主题感兴趣,可以阅读更多here.
为了演示的目的,我创建了包含 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/>
。您可以像那样覆盖任何模板部分。
编辑 - 回复评论
but can't exactly use it because I can't use {% form_theme form _self %}
您可以删除整个 {% form_theme %}
行并将您的自定义 {% block _myform_... %}
移动到单独的模板。然后打开 config.yml,查找 twig
部分并将模板作为资源包括在内,如下所示:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form:
resources:
- theme/custom.html.twig
Here my template is located in app/Resources/views/theme
这将在您拥有的每个表单中包含您的模板。但是由于您覆盖了特定表单的特定字段,因此它不应与其他表单交互。这样可以省去使用 {% form_theme %}
块的麻烦。
我正在使用表单生成器构建表单,我需要在某些字段后添加 <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')
))
;
}
我认为你做不到。水平线不是表单构造,因此,没有由 FormBuilder 定义的业务。
这需要在view/template
中处理{{ form_row(form.priority) }}
<hr/>
{{ form_row(form.bday) }}
IMO,您在构建器中使用 HTML 属性有点过于自由了。只有当你真的需要它们时才应该使用它们。同样,它们主要是视图问题。
编辑
您可以在表单的视图中添加此自定义,并且override the form_row
block了解此自定义
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 %}
好吧,正如我在评论中所说,一种方法是覆盖表单的特定字段。如果您对该主题感兴趣,可以阅读更多here.
为了演示的目的,我创建了包含 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/>
。您可以像那样覆盖任何模板部分。
编辑 - 回复评论
but can't exactly use it because I can't use {% form_theme form _self %}
您可以删除整个 {% form_theme %}
行并将您的自定义 {% block _myform_... %}
移动到单独的模板。然后打开 config.yml,查找 twig
部分并将模板作为资源包括在内,如下所示:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form:
resources:
- theme/custom.html.twig
Here my template is located in app/Resources/views/theme
这将在您拥有的每个表单中包含您的模板。但是由于您覆盖了特定表单的特定字段,因此它不应与其他表单交互。这样可以省去使用 {% form_theme %}
块的麻烦。