I'm using materialize for the front end and I have an issue with the rendering of checkbox, to make the materialize checkbox works I've to put the label after the input but I build the form with symfony so this puts the label before the input. Here is how I build my form :
$builder
->add('libelle')
->add('ordre')
->add('categorie')
->add('type', ChoiceType::class, array(
'choices' => array(
'Commande' => 'commande',
'Produit' => 'produit',
'Face' => 'face',
'Job' => 'job'
)
))
->add('fin', CheckboxType::class, array(
'label' => 'Fin action'
));
And I render the form like this :
{{ form_start(edit_form, {'attr': {'class': 'full'}}) }}
{{ form_widget(edit_form) }}
{{ form_end(edit_form) }}
Is there a way with the form builder or twig to render the label after the input ?
You can output label and field separately in Twig:
{{ form_label(field_name) }}
{{ form_widget(field_name) }}
Related
I'm a beginner with PHP and Symfony.
I'm trying to show each values of ring (which is entity) from database and trying to add checkbox at the end of the each ring.
My twig template only shows the checkbox in the first ring.
How do I show all the rings?
This is my controller:
public function testPage(Request $request) {
$ring = $this->ringRepository->customFindAll();
$jewel = $this->ringJewelRepository->customFindAll();
$custom = $this->customRingRepository->customFindAll();
$form = $this->createFormBuilder($ring)
->add('active', CheckboxType::class, [
'label'=>'Selection',
'required'=>false
])
->getForm();
$form->handleRequest($request);
return [
'rings'=>$ring,
'jewels'=>$jewel,
'custom' => $custom,
'form'=>$form->createView()
];
}
This is my Twig template:
{% for ring in rings %}
<div>
<div class="text-center">
<p>Ring Name: {{ ring.ring_name }}</p>
<p>Ring Type: {{ ring.ring_type }}</p>
{{ form_start(form) }}
{{ form_widget(form.active) }}
{{ form_end(form) }}
</div>
<div class="text-center">
Update Ring
</div>
</div>
{% endfor %}
Considering your use-case I think you might be looking for an EntityType, with the 'multiple' option set to true. The syntax would be as below. Make sure to import your Ring class as well.
->add('active', EntityType::class, [
'class' => Ring::class,
'multiple' => true,
'label' => 'Selection',
'required' => false,
'query_builder' => static fn (RingRepository $ringRepository) => $ringRepository->customFindAll(),
]);
You can read more about the EntityType in the symfony docs.
I’m trying to create several identical forms, but it turns out that the forms are the same, or rather their values are the same, thus only the value of the first form changes
foreach ($projectStages as $projectStage) {
$formStage = $this->createFormBuilder($projectStage)
->add('status', ChoiceType::class,
[
'choices' => [
'active' => true,
'not active' => false,
]
])
->add('save', SubmitType::class, array('label' => 'Set active ' . $projectStage->getTitle()));
$forms[$projectStage->getId()] = $formStage->getForm();
$forms[$projectStage->getId()]->handleRequest($request);
if ($forms[$projectStage->getId()]->isSubmitted()) {
$em->flush();
return $this->redirect('/update-project/' . $id);
}
$forms[$projectStage->getId()] = $forms[$projectStage->getId()]->createView();
}
in twig i did this
{% for formStage in formStages %}
{{ form_start(formStage) }}
{{ form_widget(formStage.status) }}
{{ form_end(formStage) }}
{% endfor %}
what is displayed in
HTML
It's a little hard to specify it in the title so I'm going to give a specific example.
I created form class called FlashcardType in which I have buildForm() method and following code inside
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('exampleSentence', CheckboxType::class, [
'label' => 'Example sentence'
])
->add('pronunciation', CheckboxType::class, [
'label' => 'Pronunciation'
])
->add('sortBy', ChoiceType::class, [
'label' => 'Sort by',
'choices' => [
'Date increase' => 1,
'Date decrease' => 2,
'Word alphabetically' => 3,
'Word not alphabetically' => 4
]
])
->add('save', SubmitType::class, [
'label' => 'Submit'
]);
}
So the result form will contain 2 checkboxes, one select drop-down and submit button.
Inside the controller I pass this form to view by createView() method.
And to make this question shorter let's take only this first checkbox as example.
So inside Twig file I have the code as follows:
{# ...form code... #}
<div class="form-check form-check-inline mr-4">
{{ form_widget(form.exampleSentence, {'attr': {'class': 'form-check-input'}}) }}
{{ form_label(form.exampleSentence, null, {
'label_attr': {'class': 'form-check-label'}
}) }}
</div>
{# ...form code... #}
For checkbox type (and not only) we can set options outputted in the table while creating form by formBuilder(). But many of these options we can override in Twig functions.
For this first checkbox I set label inside buildForm() but I can also set it inside {{ form_label() }} Twig function. And I can make it for many (if not for every) option and types.
So what's the difference between wrtiting options inside form builder and Twig form functions? Which of these options should we write inside builder and which of these inside Twig? Are there some good practices in this case?
Thank you in advance for all answers!
I would like to build a form with label and inputs, but the class of them should be different. Code below creates the label for the input with same attr:
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('hours', null ,
array('attr'=>
array(
'placeholder'=>'Working Hours',
'class'=>'ui-spinner-box') ) )
}
In my code above the ui-spinner-box will be outputted for both label and input. It will even put placeholder for its label.
So how to make it create attr for label separately so I can output something like below :
<label class="MYCLASSFOR_LABEL" for="input_id">Hours</label>
<input class="MYCLASSFOR_INPUTS" type="text" id="input_id" name="" value="" >
As mentioned in the documentation:
attr : A key-value array that will be rendered as HTML attributes on the field
label_attr: A key-value array that will be rendered as HTML attributes on the label
You can set those attributes in twig template or in form builder:
Twig template:
for symfony 2.1 and newer use:
{{ form_label(form.hours, null, {'label_attr': {'class': 'foo'}}) }}
in the legacy symfony 2.0 it used to be
{{ form_label(form.hours, { 'label_attr': {'class': 'MYCLASSFOR_LABEL'} }) }}
{{ form_widget(form.hours, { 'attr': {'class': 'MYCLASSFOR_INPUTS'} }) }}
Form builder
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('hours', null, array(
'label_attr' => array('class' => 'MYCLASSFOR_LABEL'),
'attr' => array('class' => 'MYCLASSFOR_INPUTS'),
));
}
This may be new, but there's an easy way to do this now:
$builder
->add('hours', null , array(
'attr'=>
array(
'placeholder'=>'Working Hours',
'class'=>'MYCLASSFOR_INPUTS')
) ,
'label_attr' => array(
'class' => 'MYCLASSFOR_LABEL'
)
);
The option you're looking for is label_attr.
This works for me in Symfony 2.3:
{{ form_row(form.hours, { 'label': 'Hours:'
,'label_attr': {'class': 'MYCLASSFOR_LABEL'}
,'attr': {'class': 'MYCLASSFOR_INPUTS'}
}
)
}}
The above is no longer correct, at least in the context I was using it. In Symfony 2.1 the solution is:
{{ form_label(form.item, label|default(null), { 'label_attr': { 'class': 'MYCLASS' } }) }}
How can I set the HTML class attribute to a form <input> using the FormBuilder in Symfony2 ?
Something like this:
->add('birthdate', 'date',array(
'input' => 'datetime',
'widget' => 'single_text',
'attr' => array(
'class' => 'calendar'
)
))
{{ form_widget(form.birthdate) }}
I want this inputfield with the attribute class set to calendar
You can do this from the twig template:
{{ form_widget(form.birthdate, { 'attr': {'class': 'calendar'} }) }}
From http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand
You can do it with FormBuilder. Add this to the array in your FormBuilder:
'attr'=> array('class'=>'span2')
The answer by Acyra lead the right way if you want to set attributes inside the controller, but has many inaccuracies.
Yes, you can do it directly with the FormBuilder by using the attr attribute (introduced here for the 2.1 version and here for the 2.0) to the array of options as follows:
->add('birthdate', 'date',array(
'input' => 'datetime',
'widget' => 'single_text',
'attr' => array('class'=>'calendar')
))
It is not true that the "functionality is broken". It works very well!
It is not true that Symfony2 applies the HTML class attribute to both the label and the input (at least from the 2.1 version).
Moreover, since the attr attribute is an array itself, you can pass any HTML attribute you want to render for the field. It is very helpful if you wanna pass the HTML5 data- attributes.
You can add it in the options of your form class:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\MyEntity',
'attr' => array(
'class' => 'form-horizontal'
)
));
}
{{ form_widget(form.content, { 'attr': {'class': 'tinyMCE', 'data-theme': 'advanced'} }) }}
Like this:
{{ form_widget(form.description, { 'attr': {'class': 'form-control', 'rows': '5', 'style': 'resize:none;'} }) }}
You can to this in Twig or the FormClass as shown in the examples above. But you might want to decide in the controller which class your form should get. Just keep in mind to not have much logic in the controller in general!
$form = $this->createForm(ContactForm::class, null, [
'attr' => [
'class' => 'my_contact_form'
]
]);
Renders the HTML widget of a given field. If you apply this to an entire form or collection of fields, each underlying form row will be rendered.
{# render a field row, but display a label with text "foo" #}
{{ form_row(form.name, {'label': 'foo'}) }}
The second argument to form_row() is an array of variables. The templates provided in Symfony only allow to override the label as shown in the example above.
See "More about Form Variables" to learn about the variables argument.