I want to customize all multiple selects of my application like that :
<select class="selectpicker show-tick" data-size="auto">
...
</select>
How should I do that?
EDIT for ncrocfer
This is my build form method:
This is not complete, lack some stuff...
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('misc')
->add('url')
->add('attachment', 'file')
->add('time_estimated')
->add('started_at')
->add('finished_at')
->add('default')
->add('deadline')
->add('priority', 'entity', array(
'class' => 'LanCrmBundle:TaskPriority',
'property' => 'title',
'multiple' => false
))
->add('project', 'entity', array(
'class' => 'LanCrmBundle:Project',
'property' => 'title',
'multiple' => false
))
->add('category', 'entity', array(
'class' => 'LanCrmBundle:TaskCategory',
'property' => 'title',
'multiple' => false
))
->add('user', 'entity', array(
'class' => 'LanSecurityBundle:User',
'property' => 'username',
'multiple' => false
))
->add('products', 'entity', array(
'class' => 'LanCrmBundle:Product',
'property' => 'title',
'multiple' => true
))
;
}
Use the attr attribute in your form buider :
<?php
namespace Foo\BarBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class FooType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('foo', 'choice', array(
'choices' => array(0 => 'Option 1', 1 => 'Option 2'),
'attr' => array('class' => 'selectpicker show-tick', 'data-size' => 'auto'),
))
;
}
}
Edit
If you have a lot of forms like you said in your comment, you can use the form customization.
Create a new template file :
{# src/Foo/BarBundle/Resources/views/Form/fields.html.twig #}
{% block choice_widget_collapsed %}
{% spaceless %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %} class="selectpicker show-tick" data-size="auto">
{% if empty_value is not none %}
<option value="">{{ empty_value|trans({}, translation_domain) }}</option>
{% endif %}
{% if preferred_choices|length > 0 %}
{% set options = preferred_choices %}
{{ block('choice_widget_options') }}
{% if choices|length > 0 and separator is not none %}
<option disabled="disabled">{{ separator }}</option>
{% endif %}
{% endif %}
{% set options = choices %}
{{ block('choice_widget_options') }}
</select>
{% endspaceless %}
{% endblock choice_widget_collapsed %}
Then import your template inside your application configuration :
# app/config/config.yml
twig:
form:
resources:
- 'FooBarBundle:Form:fields.html.twig'
The change will be effective on all your select.
Use attr attribute in FormBuilder:
'attr' => array('class'=>'selectpicker show-tick', 'data-size'=>'auto')
Related
my form 'Donacion' have a relationship with 'pajuela' OneToMany.
When i add 'Pajuela' with addTagForm, it is created, but if i submit the form and i do dump(formulario), the ArrayCollection 'pajuelas' is empty.
I have read the official documentation but not found my problem...
This is the link: https://symfony.com/doc/3.3/form/form_collections.html
This is my code:
# DonacionType
...
case 4:
$builder
->add('pajuelas',CollectionType::class, array(
'entry_type' => PajuelaType::class,
'entry_options' => array(
'tipo_sede' => $options['tipo_sede'],
'sede_id' => $options['sede_id'],
),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
->add('observacionesPajuelas', TextareaType::class)
;
break;
...
# PajuelaType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('tipo', ChoiceType::class, array(
'choices' => array(
'Completa' => 'Completa',
'Controles' => 'Controles'
)
))
;
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Pajuela::class,
'tipo_sede' => null,
'sede_id' => null,
));
}
#Twig:
...
{% form_theme formulario _self %}
{% block _entidadbundle_donacion_pajuelas_entry_widget %}
{% if form is defined %}
{% if form.parent != null %}
<div class="form-group {% if form_errors(form.tipo) %}has-error{% endif %} col-lg-2 col-md-12 col-sm-12">
{% if form_errors(form.tipo) %}
{{ form_label(form.tipo, 'pajuela.tipo' | trans) }}
{{ form_widget(form.tipo, {'attr': {'class': 'form-control error'}}) }}
<label for="{{ form.tipo.vars.id }}"
class="error">{{ form_errors(form.tipo) }}</label>
{% else %}
{{ form_label(form.tipo, 'pajuela.tipo' | trans) }}
{{ form_widget(form.tipo, {'attr': {'class': 'form-control'}}) }}
{% endif %}
</div>
{% endif %}
{% endif %}
{% endblock %}
<div class="pajuelas" data-prototype="{{ form_widget(formulario.pajuelas.vars.prototype)|e('html_attr') }}">
{% for pajuela in formulario.pajuelas %}
<div class="row pajuela pajuela-persitida">{{ form_widget(pajuela) }}</div>
{% endfor %}
</div>
...
I can not find what is the problem that I am having.
I have an Entity Product to which I'm adding traductions (collection of ProductDescription).
Here is my ProductDescriptionType :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('description', 'textarea')
->add('metaDescription', 'textarea')
->add('metaKeyword', 'textarea')
->add('tag')
->add('languageId', 'hidden')
;
}
And my ProductType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('upc')
->add('model')
->add('sku')
->add('quantity')
->add('price')
->add('status', 'choice',
array(
'choices' => Product::getStatusArray(),
'required' => true,
)
)
;
//ProductDescription
$builder->add('traductions', 'collection',
array(
'type' => new ProductDescriptionType(),
'allow_add' => true,
'prototype' => true,
'label' => false,
'by_reference' => false
)
);
}
And in my view new.html.twig, I'm doing :
{% for language in languages %}
<div role="tabpanel" class="tab-pane" id="product-traduction-{{ language.languageId }}">
{{ form_row(create_form.traductions.vars.prototype.languageId, {'value' : language.languageId }) }}
{{ form_row(create_form.traductions.vars.prototype.name) }}
{{ form_row(create_form.traductions.vars.prototype.description) }}
{{ form_row(create_form.traductions.vars.prototype.metaDescription) }}
{{ form_row(create_form.traductions.vars.prototype.metaKeyword) }}
{{ form_row(create_form.traductions.vars.prototype.tag) }}
</div>
{% endfor %}
I'm not looping through create_form.traductions because it's empty (creation of a product), and I would like to have the fields of the ProductDescription already showing for all languages available.
The problem is that form_row only render the fields once.
I saw that this part is explained on the CookBook http://symfony.com/doc/current/cookbook/form/form_collections.html, but it's done with JQuery and I have to do a lot of modification to the prototype inputs to achieve what I want (replace name, put languageId in the hidden input, etc).
Can someone help me please ?
Thanks for your time.
I dont know why not can show a variable, I read the manual and I searched in many websites, but I have no idea what I'm doing wrong...
I have a classType (there is not from a entity), the class form have 3 combobox and a textbox, when I submit the form, another template (show2.html.twig) render the new form where I want show the textbox value... I speend many time and a have 0 result ;)
When I see the Symfony profiler, I can see the values of the form in the "Request POST Parameters" section, but I'cant catch it and show it in the twig template...
that is the code wath dont work for my
$this->get('request')->request->get('campo', 'can not show it ¬¬')
in the template dont show the "campo" value, just "can not show it ¬¬"
this is my classType
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('Pais', 'entity', array(
'class' => 'UnadeniZonaBundle:Pais',
'property' => 'paisnomb',
))
->add('Provincia', 'entity', array(
'class' => 'UnadeniZonaBundle:Provincia',
'property' => 'provnomb',
))
->add('Ciudad', 'entity', array(
'class' => 'UnadeniZonaBundle:Ciudad',
'property' => 'ciudnomb',
))
->add('Campo', 'text', array(
'label' => 'campo',));
}
public function getName() {
return 'ciudad2';
}
}
My controller...
public function newAction(Request $request) {
$form = $this->createForm(new Ciudad2Type());
return $this->render('UnadeniZonaBundle:Ciudad2:new.html.twig', array(
'form' => $form->createView(),
'mensaje' => 'test'
));
}
public function showAction(Request $request) {
if ($request->isMethod('POST')) {
return $this->render('UnadeniZonaBundle:Ciudad2:show2.html.twig', array(
'mensaje' => $this->get('request')->request->get('campo', 'can not show it ¬¬')
));
}
}
this the templates
(new.html.twig)
{% extends '::base.html.twig' %}
{% block content -%}
<form action="{{ path('ciudad2_show') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" />
{{ mensaje }}
</form>
{% endblock %}
(Show2.html.twig)
{% extends '::base.html.twig' %}
{% block content -%}
{{ mensaje }}
{% endblock %}
If you try this :
$postData = $request->request->get('ciudad2');
$targetValue = $postData['Campo'];
return $this->render('UnadeniZonaBundle:Ciudad2:show2.html.twig', array(
'mensaje' => $targetValue)
));
It's a typo issue. Change campo to Campo in your controller.
if ($this->getRequest()->isMethod('POST')) {
$request = $this->getRequest->request;
$campo = $request->get('Campo', 'cannot show it.', true);
return $this->render('UnadeniZonaBundle:Ciudad2:show2.html.twig', array(
'mensaje' => $campo
));
}
I have an interesting problem. I couldn't find any solution in stackoverflow and also google. I have an Entity User and User have some metas. So I created a UserMeta Entity and also UserMetaValue. In user form there is lots of tabs. And I used these metas in the tabs. Some of them in first tabs, some of them in other tabs. And all tabs have their own form. When I bind a form on active tab, active tab metas update, others changed to NULL.
StudentPersonalType.php
namespace ATL\UserBundle\Form\Type;
use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
class StudentPersonalType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add("first_name", null, array(
"label" => "İsim",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add("last_name", null, array(
"label" => "Soyisim",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add("username", null, array(
"label" => "Öğrenci Numarası",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add("email", null, array(
"label" => "Email",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add('metas', 'collection', array(
'label' => "Metas",
'type' => new UserMetaType()
));
}
public function getName(){
return "personal";
}
public function setDefaultOptions(OptionsResolverInterface $resolver){
$resolver->setDefaults(array(
"data_class" => "ATL\UserBundle\Entity\User"
));
}
}
StudentEducationType.php
namespace ATL\UserBundle\Form\Type;
use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
class StudentEducationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('metas', 'collection', array(
'label' => "Metas",
'type' => new UserMetaType(),
'by_reference' => false
));
}
public function getName(){
return "education";
}
public function setDefaultOptions(OptionsResolverInterface $resolver){
$resolver->setDefaults(array(
"data_class" => "ATL\UserBundle\Entity\User"
));
}
}
And twig
<div id="personal-info" class="tab-pane row-fluid active">
<form style="margin:20px 0 0 0;" class="ajaxForm form-horizontal form-row-seperated" action="{{ formAction }}">
{{ form_row(form.first_name) }}
{{ form_row(form.last_name) }}
{{ form_row(form.email) }}
{{ form_row(form.username) }}
{% for meta in form.metas %}
{% if meta.value.vars.label in formValues.personal %}
{{ form_widget(meta) }}
{% endif %}
{% endfor %}
{{ form_row(form._token) }}
<div class="form-actions" style="margin-bottom:0;">
<button class="btn blue" type="submit"><i class="icon-ok"></i> Kaydet</button>
</div>
</form>
</div>
<div id="education-info" class="tab-pane row-fluid">
<form style="margin:20px 0 0 0;" class="ajaxForm form-horizontal form-row-seperated" action="{{ formAction }}">
{% for meta in educationForm.metas %}
{% if meta.value.vars.label in formValues.education %}
{{ form_widget(meta) }}
{% endif %}
{% endfor %}
{{ form_row(educationForm._token) }}
<div class="form-actions" style="margin-bottom:0;">
<button class="btn blue" type="submit"><i class="icon-ok"></i> Kaydet</button>
</div>
</form>
</div>
I filter collection fields by check it in twig file with IF statement.
I ask my question again, How could I use metas in different form in same page without affecting the others?
You have done half the job of filtering out the irrelevant fields for rendering. But you also need to filter out the irrelevant fields for form binding.
When you bind the request to the form it is expecting values for every UserMeta entity because there is a field for all of them in the UserMetaType collection. You'll need to remove all the UserMetaType forms which don't correspond to one of your submitted values. It's probably best to this with a FormEvents::PRE_BIND listener.
You can see a simpler example of this at Form: Avoid setting null to non submitted field. It will be slightly more complex for you, because you'll have to iterate through the collection of UserMetaType forms and remove the ones which you don't want bound.
The problem as it is. I'm trying to set defalut form values, and there's no way i can achieve this.
Page layout
{% extends bundle ~ "Bundle::reg.html.twig" %}
{% form_theme regForm 'AcmeBundle:Form:order_type_fields.html.twig' %}
{% block content %}
<form action="{{ path('reg') }}" {{ form_enctype(regForm) }} method="POST">
<div>
{{ form_widget( regForm.firstname )}}
</div>
{{ form_rest( regForm )}}
<input type="submit>
</form>
{% endblock %}
fields layout
{% block text_widget %}
{% spaceless %}
<label>{{label}}</label>
<input type="text" {{ block('widget_attributes') }} value="{{ value }}">
{% endspaceless %}
{% endblock text_widget %}
Controller
$user = new User();
$regForm = $this->get('form.factory')->create(new RegForm(), $user);
return $this->render("FrontendBundle:Order:type.html.twig", array(
'regForm' => $regForm->createView(),
);
Form
class RegForm extends AbstractType
{
public function buildForm( FormBuilder $builder, array $options ){
$builder->add( 'firstname', 'text', array( 'label' => ' ', 'data' => 'Enter your name here' ) );
}
public function getName()
{
return 'reg';
}
}
I've already tried to set default field data in many ways:
in my controller by defining firstname field (
$user->setFirstname('Enter your name here');
).
In form builder by using setData method (
$builder->add( 'firstname', 'text', array( 'label' => ' ' ) )->setData(array('firstname' => 'Enter your name here'));
Or just
$builder->setData(array('firstname' => 'Enter your name here'));
All other form stuff work's just fine. For example, i can set label 'FIRSTNAME' without any problems. So, what am i missing?
UPD: After first answer i decided to ask a direct question:
Why doesn't work default value for this, while label works just fine:
$builder->add('firstname', 'text', array('label' => 'name', 'data' => 'Andrew'));
?
Maybe some mistakes in twig templates?
Have you tried this in your Form Type:
$builder
->add('firstname','hidden', array(
'attr' => array(
'value' => 'Enter your name here',
),
))
It seems to me, though, that you want to use a placeholder (HTML5) instead:
$builder
->add('firstname','hidden', array(
'attr' => array(
'placeholder' => 'Enter your name here',
),
))
In your controller, set the data on the $user object before creating the form
$user = new User();
$user->setFirstname('Default name');
$regForm = $this->get('form.factory')->create(new RegForm(), $user);
BTW you might want to use the placeholder attribute on the input:
<input placeholder="Enter your name here">
You can do this by adding the 'attr' option on the form builder:
$builder->add('firstname', 'text', array(
'attr' => array('placeholder' => 'Enter your name here'),
));