I'm using the symfony forms for creating a select-box with all my users. I display them by fullname, but want to sort them alphabetically.
$builder->add('transferTo', 'document', [
'class' => 'UserBundle:User',
'property' => 'fullname',
'label' => 'Overdragen aan',
'attr' => ['class' => 'form-control selectpicker'],
'label_attr' => ['class' => 'col-sm-2 control-label'],
'multiple' => false,
'required' => true
]);
How can i sort the users alphabetically on fullname of firstName?
What you need is to add queryBuilder to the form params
use Doctrine\ORM\EntityRepository;
$builder->add('transferTo', 'document', [
'class' => 'UserBundle:User',
'query_builder' => function(EntityRepository $repository) {
return $repository->createQueryBuilder('u')->orderBy('u.fullname', 'ASC');
}
'property' => 'fullname',
'label' => 'Overdragen aan',
'attr' => ['class' => 'form-control selectpicker'],
'label_attr' => ['class' => 'col-sm-2 control-label'],
'multiple' => false,
'required' => true
]);
I assumed the fieldname in your entity is u.fullname
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With