Actually I display a field of many projects with radio button. This option is not required so I would to be able to reset the project selection (like checkbox but without multiple selection).
# MyNiceBundle/Form/Type/TsakType.php
// This code display all project with radio button
$builder->add('project', 'entity', array(
'class' => 'MyNiceBundle:Project',
'property' => 'name',
'multiple' => false,
'required' => false,
'expanded' => true,
'query_builder' => function(EntityRepository $er) use ($user) {
return $er->createQueryBuilder('p')
->innerJoin('p.collaborations', 'pc')
->andWhere('pc.participant = :participant')
->setParameter('participant', $user);
})
);
Is there an option to do that or I need to use an another widget?
Thanks in advance.
Actually a selectable, empty value should be displayed in this case (like with "expanded" => false). Unfortunately, this is a known bug and not fixed yet: https://github.com/symfony/symfony/issues/3154
Related
I have a selection of events provided by query builder. All entities have to be shown in the selection field, using the future events as preferred choices. Both parts should be sorted by date (asc).
Till now it worked fine the following way:
->add('event', EntityType::class, array(
'class' => 'App:Event',
'choice_label' => 'name',
'expanded' => false,
'multiple' => false,
'required' => false,
'placeholder' => '-',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('e')
->orderBy('e.date', 'ASC');
},
'preferred_choices' => function ($event) {
if($event->getDate() > new \DateTime()){
return true;
}
return false;
}
))
Since the update to Symfony 4.3 the sort order of the preferred choices is no longer kept, as described here:
https://github.com/symfony/symfony/pull/30985
I've no array of the preferred choices, as I use a callable to decide which event is in the future. How can I sort the preferred choices now? I couldn't find any clue for that in the changing description.
Is there really no longer any other way than creating an array of events outside the formbuilder?
How to set a preferred choice in a form for radio button ?
What I need is to have a specific radio checked (the last of my 4 radio button) when I load the web page...
But I read that "preferred choice" is used just for a choice list not for radio or checkbox...
http://symfony.com/doc/current/reference/forms/types/choice.html#preferred-choices
I tried that:
->add('Offre', 'entity', array(
'class' => 'AVCMediasBundle:Offre',
'query_builder' => function (EntityRepository $repository) use ($parametre_langue) {
return $repository->createQueryBuilder('q')
->where('q.id = 4')
->orderBy('q.prix', 'DESC');
},
'property' => 'titre' . $parametre_langue,
'expanded' => true,
'multiple' => false,
'label' => false,
'preferred_choices' => array('1'),
'attr' => array(
'class' => 'langue_selector'
)
));
Thanks
My solution was to passed my choice directly in the entity when I load my form
$preferedOffer = $em->getRepository('AVCMediasBundle:Offre')->findOneById('4');
$DetailTransaction->setOffre($preferedOffer);
I try to add dinamically a form input Elemnt on onchange of select element.
I have this code:
$this->addElement('select', 'nationality', array(
'multiOptions' => array('CH' => 'Choose','IT' => 'Italy', 'other' => 'Other'),
'required' => true,
'label' => 'Nation',
'filters' => array('StringTrim'),
'onChange' => '???'
));
I try to paste my new element instead "???"
$this->addElement('select', 'nationality', array(
'multiOptions' => array('CH' => 'Choose','IT' => 'Italy', 'other' => 'Other'),
'required' => true,
'label' => 'Nation',
'filters' => array('StringTrim'),
'onChange' => '$this->addElement(
'text', 'codice_fiscale', array(
'required' => true,
'label' => 'Codice fiscale',
'required' => true
))'
));
I know is not correct, but I dont' found any documentation about it.
How can I add an element onchange select value?
Thanks in advance
I would approach the problem in a different way, if it's possible for you:
javascript functions to call on the onChange. It would be syntax correct (and, as you said, what you wrote is not correct, it's going to be printed out entirely on the onChange attribute, not interpreted through php).
so, i would write
'onChange' => 'addElement()'
and then declare the javascript function which handles new elements. Or, you can prepare the elements before (if you have a fixed number), and show / hide them. It may be convenient if you don't have much elements, but if they're too many, just add through javascript the needed one.
PseudoCode Javascript
function addElement()
{
// add your element here, or show / hide it
}
As far as I saw from my experience and similar cases
(Zend_form_element_select onchange in zend framework,
Zend Form: onchange select load another view content)
you won't escape from Javascript if you want to handle the "onChange"
i am making a shopping website and i got a probleme with some form. it's about the size part. I am gonna show some screenshots it's gonna be easier to understand :
what type of field is this kind of form ?
and then i would like the get this to show a list for client
i dont ask you to do the job for me but if you had some stuff (like a tutorial or some documentation about this kind of form) or some cluee i would really appriciate
Thx !
I'm not sure, but I think you are looking for Symfony's Form Types Reference.
In this case, you are probably talking about a choice field. A choice field can be rendered as a set of checkboxes / radio buttons (as in the first image), or as a <select> element (dropdown list), as in the second image, depending on wether the expanded option is set to true or false.
Radio buttons:
$builder->add('size', 'choice', array(
'choices' => array('s' => 'S', 'm' => 'M', 'l' => 'L', 'xl' => 'XL'),
'expanded' => true,
));
Dropdown list:
$builder->add('size', 'choice', array(
'choices' => array('s' => 'S', 'm' => 'M', 'l' => 'L', 'xl' => 'XL'),
'expanded' => false,
));
The default value of expanded is false, so if you don't specify it, the field will be rendered as a dropdown list.
If you have all available sizes stored in a table in your database, you might want to look at the entity field type as well. The entity type basically extends the choice type, but retrieves the available choices from the database:
$builder->add('size', 'choice', array(
'class' => 'MyWebshopBundle:Size',
'property' => 'name',
'expanded' => false,
));
This will fetch all Size entities from the database, and uses their name property in the dropdown list.
It's a choice type field
$form = $this->createFormBuilder($data)
->add('size', 'choice',
array('choices' => array(
1 => 'X',
2 => 'XL',
...
),
'data' =>'selectionnez la taille'
))
->getForm();
The option multiple can switch between radio button (false) and checkboxes ( true)
If you prefer a list like this you have to set the expanded attribute to false, true gives checkboxes or radios.
http://symfony.com/doc/current/reference/forms/types/choice.html
This is my form
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('nombreBD','text', array( 'required' => true, 'label' => 'Nombre Base de Datos: '))
->add('Servidor', 'choice', array(
'choices' => array('1' => 'Si', '0' => 'No'),
'required' => true,
'multiple' => false,
'expanded' => true,
'label' => 'Servidor Existente?'
))
->add('ServidorBD','entity',
array ('class' => 'MonseWebBundle:ServidoresBD',
'multiple' => true,
'required' => true,
'label' => 'Servidor de Base de Datos: ',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.url', 'ASC');
},
))
;
}
and what i'm trying to do is if the User chooses "No" in the radio button, the "ServidorBD" entity shouldn't display and it should display instead another form (loading it dynamically or redirecting the user to another url) to add a new one.
Since i'm new to Symfony2, i don't quite understand how to attach the eventlistener to the "radio button" nor how to display another bit of form instead of the "ServidorBD" when this happens.
PLEASE HELP! T-T
What you want to do is build your form dynamically according to datas you will bind on your form.
In Symfony 2.0.x or 2.1.x, the form component is not able to to alter the form structure after binding datas on it. That will be done in Symfony 2.2.
See this issue: https://github.com/symfony/symfony/issues/3767
So, currently, you can't use the form event listener to archive this use case.