Is there any way to determine current action (create or edit) in Sonata\AdminBundle\Admin\Admin::configureFormFields()?

前端 未结 6 1135
深忆病人
深忆病人 2021-02-12 19:35

I\'d like to create different fields configuration for create and edit actions in Sonata Admin Bundle.

Is there any way to determine it except checking $this->g

相关标签:
6条回答
  • 2021-02-12 19:53

    In sonata admin from version 3.x

      if ($this->isCurrentRoute('create')) {
        // CREATE
      }
      else {
        // EDIT
      }
    

    In sonata admin before version 3.x use:

      $subject = $this->getSubject();
      if ($subject->isNew()) { 
        // CREATE
      }
      else {
        // EDIT
      }
    
    0 讨论(0)
  • 2021-02-12 19:55

    You can also do this:

    protected function configureFormFields(FormMapper $formMapper) {
      if ($this->isCurrentRoute('create')) {
        // CREATE
      }
      else {
        // EDIT
      }
    }
    
    0 讨论(0)
  • 2021-02-12 19:56
    public function getAction(): ?string
    {
        if (! $this->getRequest()) {
            return null;
        }
        $pathArray = \explode('/', $this->request->getPathInfo());
    
        return \end($pathArray);
    }
    
    0 讨论(0)
  • 2021-02-12 19:59

    with:

    if($this->getRequest()->get($this->getIdParameter()) == null){
       // create
    } else {
       // edit
    }
    
    0 讨论(0)
  • 2021-02-12 20:10

    You can also do this:

    protected function configureFormFields(FormMapper $formMapper) {
      if ($this->isCurrentRoute('create')) {
        // CREATE
      }
      else {
        // EDIT
      }
    }
    
    0 讨论(0)
  • 2021-02-12 20:13

    I use this :

    $creationMode = ($this->id($this->getSubject()))?(false):(true);
    if ($creationMode){
     //Ok
    }
    
    0 讨论(0)
提交回复
热议问题