D
D
dev2expert2015-03-26 15:09:46
symfony
dev2expert, 2015-03-26 15:09:46

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

This is called by the controller for the entity in the chain:
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;
}

Here everything was done using CRUD, the logic did not change.
The essence of the task is to be able to select users and contacts in the modal window, and not through the default select / checkboxes.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dev2expert, 2015-03-30
@dev2expert

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

because the one in the documentation gave an error:
The controller must return a response (Array(result => Array()) given).

S
skynetdev, 2015-03-28
@skynetdev


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?

The best mechanism when you assume that there will be too many users and it will be difficult to select a user in a simple select is
a standard mechanism called Autocomplete which can be implemented in symphony

A
Alexey Pavlov, 2015-03-26
@lexxpavlov

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 question

Ask a Question

731 491 924 answers to any question