Symfony - Form - preferred choice for radio button - forms

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);

Related

Doctrine ChoiceType setting a default value on form load

I have created a form on a Symfony CRM using the buildForm() function in a form type file. This form includes a choice drop down consisting of simple "yes" and "no" options which map to 1 and 0 respectively. I need to be able to have "no" as the default as my client more often will select this option over the "yes". After reading the documentation here I figured that the preferred_choices option would suit my needs.
Here is my entry in the buildForm() :
$builder->add('non_rider', ChoiceType::class,
array(
'label' => 'Is Non-Rider',
'required' => true,
'placeholder' => false,
'choices' => array(
'Yes' => 1,
'No' => 0
),
'preferred_choices' => array(0,1),
'label_attr' => array(
'class' => 'control-label'
),
'attr' => array(
'class' => 'form-control required'
)
));
However, this brings out the order as "Yes" then "No" with "Yes" as the default selected option. I was wondering if it reads 0 as null, which means it doesn't register? Is there any way to make "No" the auto-selected option on form load?
You can use the "data" option as mentioned here symfony.com/doc/current/reference/forms/types/choice.html, and show in action here http://stackoverflow.com/a/35772605/2476843
$builder->add('non_rider', ChoiceType::class,
array(
'label' => 'Is Non-Rider',
'required' => true,
'placeholder' => false,
'choices' => array(
'Yes' => 1,
'No' => 0
),
'data' => 0,
'preferred_choices' => array(0,1),
'label_attr' => array(
'class' => 'control-label'
),
'attr' => array(
'class' => 'form-control required'
)
));

Create a SelectBox using Form Helper in CakePHP3

I am trying to make a combo box for an edit page.
echo $this->Form->select('status',
['empty' => 'Select Status'],
['class' => 'form-control', 'required']
);
Here I want to add 2 things :
$options = array('0' => 'Inactive',
'1' => 'Active',
);
and selected value. suppose that is $status;
I tried with different options but sometime it do not add classes and sometime it shows options in tag
It will be great if somebody give clue.
Thanks
<?= $this->Form->input('status', [
'type' => 'select',
'options' => ['0' => __('Inactive') , '1' => __('Active')],
'empty' => __('Select Status'),
'class' => 'form-control',
'required' => true, 'label' => __('Type')
])
?>

Propel form type model w. group_by is rendered without property display

Env: Symfony2 2.7 / Propel 1.6
I've created a choice form type like that:
$builder->add('mychoice', 'model', array(
'class' => 'Foo\\Bar',
'query' => FooBarQuery::create()->filterBySomething(true),
'group_by' => 'example',
'property' => 'title',
'multiple' => false,
'expanded' => false,
));
The rendering choice list is ok with good optgroup select options but the title's property is not displayed - id's property instead. If I remove the group_by option, the title property is well displayed.
What's wrong?
Would this work?
$builder->add(
'mychoice',
'entity',
array(
'class' => 'Foo\\Bar',
'choice_label' => 'title',
'multiple' => false,
'expanded' => false,
)
);
Set the type to entity and add a choice_label property and the property you want to be displayed.

How to implement the event listener to a radio button on Symfony2?

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.

How to unselect radio button selection in form?

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