简体   繁体   中英

Sonata admin form collection

How to limit the number of embedded form with the type " sonata_type_collection " ?

$formMapper->add('phones', 'sonata_type_collection',
                        array(
                            'required' => true,
                            'by_reference' => false,
                            'label' => 'Phones',
                            ),
                        array(
                            'edit' => 'inline',
                            'inline' => 'table'
                            )

I would like limit to last five phones, I found only this solution for now, limit the display in the template twig " edit_orm_one_to_many ", but i don't like that.

I found a solution by rewriting the edit action in the controller, such in the documentation sonataAdminBundle I created my admin controller class:

class ContactAdminController extends Controller
{

    public function editAction($id = null)
    {
        // the key used to lookup the template
        $templateKey = 'edit';

        $em = $this->getDoctrine()->getEntityManager();
        $id = $this->get('request')->get($this->admin->getIdParameter());

        // $object = $this->admin->getObject($id);
        // My custom method to reduce the queries number
        $object = $em->getRepository('GestionBundle:Contact')->findOneAllJoin($id);

        if (!$object)
        {
            throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
        }

        if (false === $this->admin->isGranted('EDIT', $object))
        {
            throw new AccessDeniedException();
        }

        $this->admin->setSubject($object);

        /** @var $form \Symfony\Component\Form\Form */
        $form = $this->admin->getForm();
        $form->setData($object);

        // Trick is here ###############################################
        // Method to find the X last phones for this Contact (x = limit)
        // And set the data in form
        $phones = $em->getRepository('GestionBundle:Phone')->findLastByContact($object, 5);
        $form['phones']->setData($phones);
        // #############################################################

        if ($this->get('request')->getMethod() == 'POST')
        {
            $form->bindRequest($this->get('request'));

            $isFormValid = $form->isValid();

            // persist if the form was valid and if in preview mode the preview was approved
            if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved()))
            {
                $this->admin->update($object);
                $this->get('session')->setFlash('sonata_flash_success', 'flash_edit_success');

                if ($this->isXmlHttpRequest())
                {
                    return $this->renderJson(array(
                        'result'    => 'ok',
                        'objectId'  => $this->admin->getNormalizedIdentifier($object)
                    ));
                }

                // redirect to edit mode
                return $this->redirectTo($object);
            }

            // show an error message if the form failed validation
            if (!$isFormValid)
            {
                $this->get('session')->setFlash('sonata_flash_error', 'flash_edit_error');
            }
            elseif ($this->isPreviewRequested())
            {
                // enable the preview template if the form was valid and preview was requested
                $templateKey = 'preview';
            }
        }

        $view = $form->createView();

        // set the theme for the current Admin Form
        $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());

        return $this->render($this->admin->getTemplate($templateKey), array(
            'action' => 'edit',
            'form'   => $view,
            'object' => $object,
        ));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM