Reputation: 4038
I am building a form using the formbuilder, where I need to add <hr/>
after certain fields. How can I do that? FYI, Need to add <hr/>
after the field priority
and another before biography
. My form builder:
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')
))
;
}
Upvotes: 2
Views: 2640
Reputation: 105914
I don't think you can. A horizontal rule is not a form construct, and as such, has no business being defined by a FormBuilder.
This needs to be handled in the view/template
{{ form_row(form.priority) }}
<hr/>
{{ form_row(form.bday) }}
IMO, you've been a bit too liberal with your use of the HTML properties in your builder. Those should be used only if you really need them. Again, they are primarily view concerns.
You can add this customization in the form's view, and override the form_row
block to be aware of this customization
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);
}
}
Then the form block
{% block form_row %}
{{ parent() }}
{% if form.use_separator is defined %}
<hr/>
{% endif %}
{% endblock %}
Upvotes: 3
Reputation: 3762
All right, as I said in comments, one way would be to override specific field of your form. If you are interested in the subject, you can read more here.
For the purpose of demonstrating, I've created simple form with 3 fields, as follows:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('username', 'text');
$builder->add('email', 'email');
$builder->add('password', 'password');
}
Now, the important part here is the name of the form (it's used later when overriding)
public function getName() {
return 'myform';
}
Nothing new here, most of the magic happens in your template file.
{% form_theme form _self %}
The following line tells Symfony that you are using the same template file as a base to create a form theme. Next:
{% block _myform_email_row %}
{{ block('form_row') }}
<hr/>
{% endblock %}
This block is all you need to define at the top of your template, after {% form_theme ... %}
{% block _myform_email_row %}
_myform
is the name of your form obviously._email
is the name of the input you want to override_row
is what part of it you want to override. row
stands for label, widget and block. _widget
would override only the part that renders the input.And that's all you need. Define custom block, render the default block and add <hr/>
at the end of it. You can override any template part like that.
Edit - response to a comment
but can't exactly use it because I can't use {% form_theme form _self %}
You can delete the whole {% form_theme %}
line and move your custom {% block _myform_... %}
to separate template. Then open your config.yml, look for the section twig
and include the template as a resource, like this:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form:
resources:
- theme/custom.html.twig
Here my template is located in app/Resources/views/theme
This will include your template in every form you have. But since you override specific field of specific form, it should not interact with other forms. This way would save you the trouble of using {% form_theme %}
block.
Upvotes: 2