Answer the question
In order to leave comments, you need to log in
Symfony2: How to make a selection in a modal window in a form?
There is a document with a form in which there is a field with a choice of the user.
One-to-many relationship (one user can be selected in different documents).
The list of users can be displayed in the form of a select or a set of check boxes, but if there are more than 10 of them, then this is no longer convenient, not to mention numbers of a higher order.
Tell me how best to implement the user's choice, for example, through a modal window with a listview of users, which will be opened by clicking on the button?
So far, I have an idea to only hide the current User field, but just don’t hide it, apparently I’ll have to mess with->add('user', 'hidden',array('mapped' => false))
and then make your custom one and load the selected one into it using js, and then copy the value from this field to the User field in the controller and make an entry in the database, but then there is a problem with displaying the selected user when opening the form for editing.
Maybe there is some standard mechanism or something similar that can be rewritten.
UPD:
Apparently it was not quite clear, it has nothing to do with the admin panel, there is a document entity that was created using CRUD, the forms are slightly tweaked and in this version they work, but the selection interface is select:
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('created_date')
->add('period')
...
->add('contact')
->add('assigned_user')
;
}
public function editAction($id){
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBundle:Documents')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Sales entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('MyBundle:My:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
private function createEditForm(Documents $entity){
$form = $this->createForm(new MyType(), $entity, array(
'action' => $this->generateUrl('my_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
Answer the question
In order to leave comments, you need to log in
I took advantage of the skynetdev suggestion , modal windows in my case would really be superfluous.
Used PUGXAutocompleterBundle , but had to rewrite find method:
public function searchMyAction(Request $request){
$q = $request->get('term');
$em = $this->getDoctrine()->getManager();
$contacts = $em->getRepository('MyBundle:Contacts')->findByLikePhoneMobile($q);
$results = array();
foreach ($contacts as $contact) {
$results[] = array(
'id' => $contact->getId(),
'phone' => $contact->getPhoneMobile(),
'label' => sprintf("%s", $contact->getName())
);
};
return new JsonResponse($results);
}
The controller must return a response (Array(result => Array()) given).
The list of users can be displayed in the form of a select or a set of check boxes, but if there are more than 10 of them, then this is no longer convenient, not to mention numbers of a higher order.
Tell me how best to implement the user's choice, for example, through a modal window with a listview of users, which will be opened by clicking on the button?
use the sonata_type_model_list type
https://sonata-project.org/bundles/doctrine-orm-ad...
Just set the field type to sonata_type_model_list and that's it:
->add('user', 'sonata_type_model_list')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question