Empty option in a form query symfony 3.4 - forms

I must display an entity in a query_builder form
here is my code in the form type:
$builder->add('dispositif', EntityType::class, array(
'class' => 'LeaPrestaBundle:EgwDispositif',
'label' => 'nomDispositif',
'required' => true,
'empty_data' => null,
'query_builder' => function( $er)
{
return $er->createQueryBuilder('d')
->where('d.isActive = :isActive')
->setParameter('isActive', 1 )
->orderBy('d.nomDispositif','ASC');
},
));
The problem is that the option is empty :
<select id="egw_presta_dispositif" name="egw_presta[dispositif]">
<option value="52"></option>
<option value="55"></option>
<option value="62"></option>
<option value="58"></option>
</select>
For example, aside the value 52, there should have a label for "dispositif" (name of dispositif).
Does it coming from the entity ?
Thanks in advance for your help!

Try adding a 'choice_label' property with the EgwDispositif field containing what you want to display:
$builder->add('dispositif', EntityType::class, array(
'class' => 'LeaPrestaBundle:EgwDispositif',
/** label will be displayed next to your form field unless it's a translation key or else **/
'label' => 'Nom du dispositif',
'required' => true,
'empty_data' => null,
/** any field containing what you want to display as option label **/
'choice_label' => 'nomDispositif',
'query_builder' => function( $er)
{
return $er->createQueryBuilder('d')
->where('d.isActive = :isActive')
->setParameter('isActive', 1 )
->orderBy('d.nomDispositif','ASC');
},
));
It seems like you were confusing the option 'label' of the form field property with the option 'choice_label' . You probably want your label to be something user will read (bu maybe not, it's up to you)
take a look here : https://symfony.com/doc/current/reference/forms/types/entity.html

Related

Symfony give class to option entitytype element

i have an entity type with field (nome, id,id_Categoria,id_tipo)
id_tipo has value 1,2,or 3
$builder->add('idTipologiaEsame', EntityType::class, array(
'placeholder' => 'tipologia esame',
'label' => false,
'mapped' => false,
'required' => false,
'class' => 'AppBundle:Nome_esame',
'choice_label' => 'nome',
'group_by' => 'idCategoriaEsame.tipo',
))
My goal is give a class to option value in select if id_tipo is 1 or 2.
like this:
<select>
<option class="ruminanti">val 1</option> ->id_tipo=1
<option class="pippo">val 2</option> ->id_tipo=2
<option class="ruminanti">val 3</option> ->id_tipo=1
</select>
it is possible??
thank's to Ramazan Apaydın i add this
$builder->add('idTipologiaEsame', EntityType::class, array(
'placeholder' => 'tipologia esame',
'label' => false,
'mapped' => false,
'required' => false,
'class' => 'AppBundle:Nome_esame',
'choice_label' => 'nome',
'group_by' => 'idCategoriaEsame.tipo',
'choice_attr' => function($val, $key, $index) {
// adds a class like attending_yes, attending_no, etc
if($index){ ----> i want id_tipo =1
return ['class' => '.ruminati';}else{
return ['class' => '.suini';
}
},
but i want add condition id_tipo=1
EntityType is a ChoiceType element. An example is available in the Symfony documentation.
https://symfony.com/doc/current/reference/forms/types/choice.html#choice-attr

How to set a class attribute to a Symfony form input with an attribut from entity linked

I have in my form a "beds" attribute, an entity linked to another "bedroom".
I would like for each input to add a class with the id of the linked entity "bedroom".
$form->add('beds', EntityType::class, array(
'class' => 'DamiasResaBundle:Bed',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('b')
->orderBy('b.id', 'ASC');
},
'choice_label' => 'id',
'label' => 'lits ',
'multiple' => true,
'expanded' =>true,
'attr' => array(
'class' => function ($bed) {
return $bed->getBedroom()->getId();
}
),
))
I have two problems:
'attr' => array('class'=>'test) return a class attribut in the div containing the input, not a class attribut in the input.
This previous code does not work and returns:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Closure could not be converted to string") in form_div_layout.html.twig at line 358.
Thank you for your help
I see you are using checkboxes.
Your code seems ok, and since you use query_builder, the values should be a Bed Entity. Note that attr is not mentioned in the EntityType documentation, and I think you need to use choice_attr instead.
Can you try this. I'm not sure if it will work or not:
$form->add('beds', EntityType::class, array(
'class' => 'DamiasResaBundle:Bed',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('b')
->orderBy('b.id', 'ASC');
},
'choice_label' => 'id',
'label' => 'lits ',
'multiple' => true,
'expanded' =>true,
'choice_attr' => function ($val) {
return ['class' => $val->getId()];
},
))
Let us know the results.
You need to customize form rendering
Previous code doesn't work because you're trying to pass Closure object instead of string value. attr array is used as field attributes, so it must contain only string values (or objects with __toString() method).
For EntityType it would rather look like this:
$form->add('beds', EntityType::class, array(
'class' => 'DamiasResaBundle:Bed',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('b')
->orderBy('b.id', 'ASC');
},
'choice_label' => 'id',
'label' => 'lits ',
'multiple' => true,
'expanded' =>true,
'choice_attr' => function (Bed $bed, $key, $index) {
return ['class' => $bed->getBedroom()->getId();];
},
))
Since choice_attr seems not to be working anymore, I am writing another solution. I wasn't able to set attribute (exactly "min") directly from formBuilder. I managed to add it with help of js. I simply searched element by ID given by formBuilder and added atribute.
Inspect your form in browser element inspector and find id.
<div id="form_deadLine">
<input type="date" id="form_deadLine_date" name="form[deadLine][date]" required="required" value="2020-02-20"/>
</div>
Id you wanna look for in my case is form_deadLine_date.
Then you wanna connect .js file to you twig template and write there simple script.
const form = document.getElementById('form_deadLine_date');
if(form){
form.setAttribute("min", "2020-01-02")
}
then my code looks like:
<div id="form_deadLine">
<input type="date" id="form_deadLine_date" name="form[deadLine][date]" required="required" value="2020-02-20"/>
</div>

How to customize EntityType option text?

I would like to display selection with options from DB. In this case, I am using EntityType with query_builder:
$builder
->add('internships', EntityType::class, array(
'class' => 'IndexBundle\Entity\Internship',
'property' => 'id',
'empty_value' => 'Choose',
'query_builder' => function (EntityRepository $er) use ($trainee) {
return $er->createQueryBuilder('i')
->where('i.trainee = :trainee')
->andWhere('i.state = :state')
->setParameter('trainee', $trainee)
->setParameter('state', 'unverified');
},
'constraints' => array(
new NotBlank(array(
'message' => 'choice.not.blank'
))
)
))
Now all is fine. I get select element with necessary options within with text of id value.
<select>
<option value="id">id</option>
...
</select>
How do I customize it?
For example I would like it to be combination of id and type table columns:
<select>
<option value="id">#id (type)</option>
...
</select>
You can use the choice_label option to customize your options.
You can either pass a function to retrieve the text you want, or you can add a getter to your entity if you reuse it at another place.
With a function:
$builder
->add('internships', EntityType::class, array(
'class' => 'IndexBundle\Entity\Internship',
'property' => 'id',
'empty_value' => 'Choose',
'query_builder' => function (EntityRepository $er) use ($trainee) {
return $er->createQueryBuilder('i')
->where('i.trainee = :trainee')
->andWhere('i.state = :state')
->setParameter('trainee', $trainee)
->setParameter('state', 'unverified');
},
'choice_label' => function ($internship) {
return '#'.$internship->getId().' ('.$internship->getType().')';
},
'constraints' => array(
new NotBlank(array(
'message' => 'choice.not.blank'
))
)
))
With a getter:
$builder
->add('internships', EntityType::class, array(
'class' => 'IndexBundle\Entity\Internship',
'property' => 'id',
'empty_value' => 'Choose',
'query_builder' => function (EntityRepository $er) use ($trainee) {
return $er->createQueryBuilder('i')
->where('i.trainee = :trainee')
->andWhere('i.state = :state')
->setParameter('trainee', $trainee)
->setParameter('state', 'unverified');
},
'choice_label' => 'idAndType',
'constraints' => array(
new NotBlank(array(
'message' => 'choice.not.blank'
))
)
))
Internship.php:
Class Internship
{
//...
public function getIdAndType()
{
return '#'.$this->id.' ('.$this->type.')';
}
}
Note:
For older Symfony versions (<= 2.6), this option was named property and use something supported by the PropertyAccessor component, so you can't use a function, only a getter for it.

Symfony2 Form entity field with query_builder

I have a form with an entity field, without the query_builder work fine, but with the query_builder the form display the result of the query but is not valid when is submited.
//tecnicosType
$builder->add('dt', 'entity', array(
'class' => 'MSKLigaBundle:Jugadores',
'label' => 'DT',
'query_builder' => function(
\MSK\LigaBundle\Entity\JugadoresRepository $er) use($equipoId){
return $er->getJugadores($equipoId);
},
'property' => 'nombreCompleto',
'empty_value' => "Sin definir",
'required' => false
))
And the function is
public function getJugadoresQueryBuilder($equipo)
{
$queryBuilder = $this->createQueryBuilder('j');
return $queryBuilder->select('j')
->where('j.equipo_id = :equipo')
->setParameter('equipo', $equipo);
}
when the form is submited return
array
'dt' =>
array
0 => string 'This value is not valid.'
If i coment the query_builder the form validate fine.
I can't find the solution, thank for any help.

at least one checkbox selected Symfony2

I am adding the following field to my form:
->add('interessi_profilo', 'entity', array(
'label' => 'Interessi (Tr)',
'class' => 'MyProfiloBundle:TipoInteresse',
'required' => true,
'multiple' => true,
'expanded' => true,
'property' => 'tipo',
'query_builder' => function(\My\ProfiloBundle\Entity\TipoInteresseRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.id', 'ASC');
},
I would like for the form to be sent only if at least one checkbox is selected and, if possible, have a tooltip that tells the user: at least one option must be selected
Try putting the Count constraint on the field holding the collection:
use Symfony\Component\Validator\Constraints\Count;
class Entity
{
/**
* #Count(min = 1, minMessage = "At least one item must be selected")
*/
private $collection;
// ...
}