Cualquiera que haya trabajado con Symfony y se haya visto en la necesidad de añadir un campo en un formulario que haga referencia a una entidad se habrá visto en la situación de que la única opción posible es usar el tipo 'entity'. Este tipo tienes dos grandes problemas:
- Solo permite mostrar el campo mediante checkboxes, radiobuttons o selects, donde todas las opciones posibles se muestran para que el usuario escoja alguna en concreto. Esto dificulta mucho la interacción en casos como el de recuperar una entidad vía AJAX y vincularla al formulario.
- La eficiencia de lo anterior es nula, porque obliga a recuperar todos los valores posibles de la entidad a la hora de mostrar el campo. Imagina que necesitas un campo usuario y tienes más de 5000 en tu base de datos. Sería inviable usar el tipo entity.
$builder->add('city', 'entity_id', array( 'query_builder' => function($repository, $id) { return $repository->createQueryBuilder('c') ->where('c.id = :id AND c.available = 1') ->setParameter('id', $id); }, 'class' => 'Gregwar\TestBundle\Entity\City', 'required' => false, 'hidden' => true ));
I think that we should add two new options
widget and delimiter to ChoiceType.widgetcan be one of the valuesselect,checkbox,radioandtextdelimitercan be any single character
widget = 'select': This is equivalent toexpanded = falseright now.widget = 'checkbox':multiplemust not be set to false. Otherwise equivalent toexpanded = true.widget = 'radio':multiplemust not be set to true. Otherwise equivalent toexpanded = true.widget = 'text': A text input is shown.- if
multipleis false, the input must equal one of the predefined choices. - if
multipleis true, the input is split by the character defined indelimiter(a comma by default), then each value is trimmed (unlesstrimis false). Each resulting input must equal one of the predefined choices.
- if



