2012-11-06 127 views
0

如何限制類型爲「sonata_type_collection」的嵌入表單的數量?索納塔管理員表單集合

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

我想限制的最後五個電話,我發現只有這個解決方案就目前而言,限制在模板樹枝「edit_orm_one_to_many」顯示,但我不喜歡這樣。

回答

0

我找到了一個解決方案通過重寫控制器的編輯操作, 例如在文件sonataAdminBundle我創造了我的管理控制器類:

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