Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9074587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:40:18+00:00 2026-06-16T18:40:18+00:00

So, in edit action of Sonata Admin I’m trying to display different form fields

  • 0

So, in edit action of Sonata Admin I’m trying to display different form fields depending on create or edit context.

Maybe some background first..

I have a Gallery entity and a CoverPhoto entity bound with OneToOne.

Gallery:

/**
 * @ORM\OneToOne(targetEntity="CoverImage", mappedBy="gallery", cascade={"all"}, orphanRemoval=true)
 **/
private $cover;

CoverImage:

/**
 * @ORM\OneToOne(targetEntity="Gallery", inversedBy="cover")
 **/
private $gallery; 

Here’s coresponding GalleryAdmin class:

class GalleriesAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
                -> add('name', null, array('label' => 'Nazwa'))
                -> add('category', 'sonata_type_model', array('label' => 'Kategoria'), array('subject' => $this -> getSubject()))
                -> add('cover', 'sonata_type_admin', array('label' => 'Okładka'))
                -> add('images', 'sonata_type_collection', array('by_reference' => false, 'label' => 'Powiązane zdjęcia'), array(
                    'edit' => 'inline',
                    'sortable' => false,
                    'inline' => 'table',
                ))
            ;
    }

    //other stuff 
}

And here we go with CoverImageAdmin:

class CoverImagesAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
                -> add('path', 'thumbnail', array('label' => 'Miniatura'))
                -> add('file', 'file', array('required' => false, 'label' => 'Plik'))
        ;
    }
}

“Thumbnail” is my custom form field which displays thumbnail (shocker). But now i want this field only appear in “edit” context.

This should be piece of cake with

$this -> getSubject()

method of Admin class and condition. Unfortunately when I call getSubject() in CoverImagesAdmin class which is used to render nested form it always returns null. The same with getParent();

Calling getForm() results in

Fatal error: Maximum function nesting level of ‘500’ reached, aborting! in
/home/flameheart/Projects/KolberPhotography/vendor/symfony/symfony/src/Symfony/Component
/OptionsResolver/Options.php on line 350

I’ve tried to call about every method of Admin and FormMapper just to determine form’s context but ended up with nothing.

Do you guys have any idea how to solve this in a clean way ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:40:19+00:00Added an answer on June 16, 2026 at 6:40 pm

    I managed to do it this way:

    protected function configureFormFields(FormMapper $formMapper)
    {
    
        if($this -> getRoot() -> getSubject() -> getCover() && $this -> getRoot() -> getSubject() -> getCover() -> getId() )
        {
            $formMapper -> add('path', 'thumbnail', array('label' => 'Miniatura', 'attr' => array('id' => 'gallery_cover_image'), 'label_attr' => array('id' => 'gallery_cover_label')));
        }
    
        $formMapper -> add('file', 'file', array('required' => false, 'label' => 'Plik'));
    }
    

    Imo, this Sonata thing really needs loads of documentation and refactoring instead of further development.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add some more conditional logic to my edit action by
I have on my controller a Create and an Edit action. I have for
I have an error on trying to access to #new or #edit action on
I am trying to alter my edit action so that a user can only
I have a form for both the new and edit action. one of the
In an Edit action in ASP.NET MVC, certain fields can be hidden from user
I made a treelist of data, in this treelist I'm trying to edit some
I am trying to implement my Edit action methods with strongly typed view which
EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
Can we change the default action of the edit selected row button? Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.