Php 如何添加<;人力资源>;在Symfony2表单生成器中?
我正在使用formbuilder构建表单,需要在某些字段后添加Php 如何添加<;人力资源>;在Symfony2表单生成器中?,php,html,symfony,twig,Php,Html,Symfony,Twig,我正在使用formbuilder构建表单,需要在某些字段后添加。我该怎么做?仅供参考,需要在优先级字段后添加,在传记之前添加另一个。我的表单生成器: public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', 'text', array( 'label' => 'Name Englis
。我该怎么做?仅供参考,需要在优先级
字段后添加
,在传记
之前添加另一个。我的表单生成器:
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) }}
然后是表单块
{% block form_row %}
{{ parent() }}
{% if form.use_separator is defined %}
<hr/>
{% endif %}
{% endblock %}
{%block form_row%}
{{parent()}}
{%如果定义了form.use_分隔符%}
{%endif%}
{%endblock%}
好吧,正如我在评论中所说的,一种方法是覆盖表单的特定字段。如果你对这门学科感兴趣,你可以读更多
为了演示,我创建了包含3个字段的简单表单,如下所示:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('username', 'text');
$builder->add('email', 'email');
$builder->add('password', 'password');
}
现在,这里的重要部分是表单的名称(稍后在重写时使用)
这里没有什么新东西,大部分魔法都发生在模板文件中
{% form_theme form _self %}
下一行告诉Symfony,您正在使用相同的模板文件作为基础来创建表单主题。下一步:
{% block _myform_email_row %}
{{ block('form_row') }}
<hr/>
{% endblock %}
这里我的模板位于app/Resources/views/theme中
这将在您拥有的每个表单中包含您的模板。但由于您重写了特定表单的特定字段,所以它不应该与其他表单交互。这种方法可以省去使用
{%form\u theme%}
块的麻烦。我知道,但我不能单独渲染字段。模板太多,位置太多。我这辈子都得写表格!:'(Twig有很多重复使用模板/片段的机制。我不得不想象其中一个可以帮助您。我在回答中添加了另一个建议您是否尝试过为该字段创建自定义模板?Symfony提供了一种覆盖表单中任何特定字段的方法,以便您可以按照自己的喜好呈现该字段。如果您感兴趣,我将我可以给你写一个答案。请写,我看看它是否有效。:)我有这个想法,但不能确切地使用它,因为我不能使用{%form\u theme form\u self%}
。谢谢你的回答,我想我可以用它来找到解决办法。@SamiaRuponti我已经用另一个建议更新了我的答案。看一看。
{% form_theme form _self %}
{% block _myform_email_row %}
{{ block('form_row') }}
<hr/>
{% endblock %}
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form:
resources:
- theme/custom.html.twig