Symfony 2.7 and rendering of choices in Twig - php

Trying to render a dropdown in Symfony 2.7.0 but I am having some issues when rendering the choices the view.
$form = $this->createFormBuilder(null)
->add('timespan', 'choice', array(
'choices' => array(90 => "3 months", 30 => "1 month")
))
->getForm();
...
return array(
'form' => $form->createView(),
);
...
Doing var_dump after this will display the values:
var_dump($form->get('timespan')->getConfig()->getOption('choices'));
But when rendering it in the view like this:
{{ form_widget(form.timespan, {'class': 'span2'}) }}
The select box becomes empty.
<select id="form_timespan" name="form[timespan]" required="required" class="span2"></select>
Any ideas why this might occur? Am I missing something?

The problem is obviously in Twig. You can debug this by editing the form theme, to see what values comes in and what is expected. The theme can be found at:
vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig
(Or you can configure Symfony to use your own.)
You're looking for {% block choice_widget %} specifically to render this.
In this case it looks like you've forgotten to use the attr key for your HTML class:
{{ form_widget(form.timespan, {'attr': {'class': 'span2'}}) }}

You forgot the attr key in your call :
{{ form_widget(form.timespan, {'attr': {'class': 'span2'}}) }}

Related

How to merge Symfony form type attributes with attributes from form_row in twig

Suppose we have Symfony form type and a row adding field
$builder->add('name', 'text', ['attr' => ['class' => 'firstName', placeholder => 'first name']]);
I want this to be merged with attributes set in twig template:
{{ form_row(form.name, {'attr':{'class':'newClass'}}) }}
Currently it does replace. What is the best way to solve it?
You can use form variables to get form class attr, then concatene another class attr:
{{ form_row(form.name, {'attr':{'class':'newClass ' ~ form.name.vars.attr.class|default('')}}) }}
EDIT:
Corrected my previous answer after reading https://symfony.com/doc/current/reference/forms/twig_reference.html#reference-form-twig-variables

form model binding in laravel 5.2

I have been reading about form model binding https://laravelcollective.com/docs/5.0/html#form-model-binding
It's very cool to populate DB values in html form.
I tried like this and this works fantastic.
{{ Form::model($university,array('url' => admin_path('universities/edit'),'id' => 'add_university','name' =>'add_university','data-validate'=>"parsley")) }}
{{ Form::label('university_name', 'University name',array('class'=>'control-label')) }}
{{ Form::text('university_name')}}
{{Form::close()}}
But the problem is here, Cause i want to add more attributes in input like class SO i am using
{{ Form::label('university_name', 'University name',array('class'=>'control-label')) }}
{{ Form::text('university_name','',array('class' => 'form-control'))}}
If i leave blank valuecolumn then nothing populate in textbox and if i using like this
{{ Form::label('university_name', 'University name',array('class'=>'control-label')) }}
{{ Form::text('university_name',$university->university_name,array('class' => 'form-control'))}}
Then what is use of model binding.
Please explain.
Thanks
{{ Form::text('university_name','',array('class' => 'form-control'))}}
It should be:
{{ Form::text('university_name',null,array('class' => 'form-control'))}}
'' means the real string, not null.
thanks, mathielo, for helping me on grammar

Handling array paramter in html_anchor function of Twig in FuelPHP

I am converting the PHP based template to TWIG based template in FuelPHP.
echo Html::anchor('categories/create', 'Add', array('class' => 'btn'));
I found out with some help that the equivalent tag for TWIG is html_anchor which works fine. But as in this case, there is an third parameter passed as an array. How that can be converted for TWIG?
I tried the below found 2 lines and both failed with error so I assume its not the correct way.
{{ html_anchor('categories/create', 'Add', array('class' => 'btn')) }}
Twig arrays has [] format, so I tried this too.
{{ html_anchor('categories/create', 'Add', ['class' : 'btn']) }}
What's the correct way of handling this?
Here's how you define an associative array in twig:
{{ html_anchor('categories/create', 'Add', { 'class': 'btn' }) }}

Symfony2 - Twig render controller. Constraints form display

I need to render a controller in a template (principal.html.twig) with #Route and #Template annotations in order to create a form:
{{ render(controller('PprsBundle:Default:SupuestoConfig'), {'strategy': 'inline'}) }
Controller:
/**
* #Route("/configsup", name="configsup")
* #Template("PprsBundle:Default:SupuestoConfig.html.twig")
*/
public function SupuestoConfigAction()
{
...
->add('number', 'text', array(
'constraints' => new Length(array(
'min' => 1,
'max' => 2,
)),
....
}
routing.yml:
configsup:
resource: "#PprsBundle/Controller"
type: annotation
SupuestoConfig.html.twig:
<form id="configurador" action="{{ path('configsup') }}" method="POST">
<p class="titulo_configurador">Elija supuesto penal:</p>
{{ form_row(form.tipo) }}
{{ form_row(form.numero, { 'label' : ' ', 'attr' : { 'class' : 'rec3' }}) }}
{{ form_rest(form) }}
<input id= "btTipoSupuesto" type="submit" value="Cargar" class="inputbt"/>
</form>
I'm having an unexpected behaviour when the constraint is activated (when I introduce a 4 digits number in "number" field) because it only renders the view SupuestoConfig.html.twig showing the constraint error (route /configsup) instead of the whole page (principal.html.twig). How can i make it work?
You are sending the form to the SupuestoConfigAction which renders only the form. You should send your form to the action where you render the form originally (where you use {{ render(controller('PprsBundle:Default:SupuestoConfig'), {'strategy': 'inline'}) }. Handle the form submission in that action and display the result.
So it's not a strange behaviour. It behaves exactly as you tell your program to.

How do you hide labels in a form class in symfony2?

I know that you can split a form out in twig and choose to not render the label for a particular field, but I can't help but think that you must be able to do this from the form class. The 'label' key in the options array lets you change this value to whatever you like, but passing either false or an empty string just returns the field name (see examples below where 'roles' is rendered as the label).
$builder
->add('roles', 'entity', array(
'class' => 'Acme\UserBundle\Entity\Role',
'label' => ''
));
$builder
->add('roles', 'entity', array(
'class' => 'Acme\UserBundle\Entity\Role',
'label' => false
));
Strangely, passing an empty space (which feels very dirty) seems to render a completely empty label, with no space even when viewing the source. Can anyone shed any light on the best approach, or even why the empty space seems to work?
Since Symfony 2.2 you can avoid the <label> rendering using the false value for the label attribute:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('Name', null, array('label' => false))
;
}
Source
Keep your 'View' specifications separate from your 'Model'
If you follow the accepted answer which says:
$builder
->add('Name', null, array('label' => false))
;
your form is not as re-usable. Especially if your form appears in more than one location (or might in the future).
If you do not want to render the form label it is best to do so in Twig (assuming your using Twig).
instead of rendering {{ form_row(form.name) }}, render each element separetly and exclude the form_label
ex.
{{ form_errors(form.name) }}
{# {{ form_label(form.name) }} <-- just dont include this #}
{{ form_widget(form.name) }}
If down the road you wanted the label in one instance of the form but the not the other, simply adding {{ form_label(form.name) }} would suffice; Where as changing array('label' => true) would turn the label on everywhere
If you are rendering your form with the one liner {{ form(form) }} then you should have a look at the symfony docs
Just add {'label':false} to your form_row()
{{ form_row(form.name, {'label':false}) }}
I don't understand very well your question but in form to show the name of label,personnaly I do like that :
$builder
->add('role', 'text')
in my twig :
<tr>
<td>{{ form_widget(form.role) }} </td>
<td>{{ form_label(form.role, "Name of Label") }}</td>
</tr>
<tr>
<td>{{ form_errors(form.role) }}</td>
</tr>
To hide my label, I had to render just the widget for the field, and not the label, e.g.
{{ form_widget(edit_form.event) }}
{{ form_rest(edit_form) }}
The problem with the ' ' label with a space in, is that it still renders the html input which is there and affects the page.
this should work (although its not a very clean solution)
$builder
->add('roles', 'entity', array(
'class' => 'Acme\UserBundle\Entity\Role',
'label' => ' '
));
(note the space between the ticks)

Categories