简体   繁体   中英

Symfony 3 - Sonata Admin Bundle - configureFormFields disable fields when added from another entity

I have 2 Entities. Company and Stores.

When i add a company, i can add a store too. My Problem is, when adding this over the company entity, i don't want to see the field 'company' on the form.

The 'company' field should only be shown when i'm adding a store directly over 'Store add'.

Is it possible, to remove or disable this field when adding a store over the company entity?

Thanks a lot.

UPDATE

this is my ClientAdmin.php

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name', null, array(
            'label' => 'Name'
        ))
        ->add('email', 'email', array(
            'label' => 'E-Mail'
        ))
        ->add('street', null, array(
            'label' => 'Straße'
        ))
        ->add('streetno', null, array(
            'label' => 'Haus Nr.'
        ))
        ->add('city', null, array(
            'label' => 'Ort'
        ))
        ->add('zip', null, array(
            'label' => 'PLZ'
        ))
        ->add('stores', 'sonata_type_model', array(
            'multiple' => true,
            'by_reference' => false,
            'class' => 'StoreBundle\Entity\Store',
            'label' => 'Filialen',
        ))
    ;
}

this is my StoreAdmin.php

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name', null, array(
            'label' => 'Name',
        ))
        ->add('number', null, array(
            'label' => 'Nummer',
        ))
        ->add('street', null, array(
            'label' => 'Straße',
        ))
        ->add('streetno', null, array(
            'label' => 'Haus Nr.',
        ))
        ->add('zip', null, array(
            'label' => 'PLZ',
        ))
        ->add('city', null, array(
            'label' => 'Stadt',
        ))
        ->add('email', null, array(
            'label' => 'E-Mail',
            'required' => false,
        ))
        ->add('client', 'sonata_type_model', array(
            'required' => false,
            'multiple' => false,
            'by_reference' => false,
            'class' => 'ifabrik\ClientBundle\Entity\Client',
            'label' => 'Unternehmen',
        ))
        ->add('editor', 'sonata_type_model', array(
            'required' => false,
            'multiple' => false,
            'by_reference' => false,
            'class' => 'UserBundle\Entity\User',
            'label' => 'Bearbeiter',
        ))
    ;
}

Ok so in your storeAdmin you can check if you already have a client in order to render the client field or not.

protected function configureFormFields(FormMapper $formMapper)
{
    $subject = $this->getSubject();
    $isNew = null === $subject->getClient();

    $formMapper
        ->add('name', null, array(
            'label' => 'Name',
        ))
        ->add('number', null, array(
            'label' => 'Nummer',
        ))
        ->add('street', null, array(
            'label' => 'Straße',
        ))
        ->add('streetno', null, array(
            'label' => 'Haus Nr.',
        ))
        ->add('zip', null, array(
            'label' => 'PLZ',
        ))
        ->add('city', null, array(
            'label' => 'Stadt',
        ))
        ->add('email', null, array(
            'label' => 'E-Mail',
            'required' => false,
        ));
    if ($isNew) {
       $formMapper
        ->add('client', 'sonata_type_model', array(
            'required' => false,
            'multiple' => false,
            'by_reference' => false,
            'class' => 'ifabrik\ClientBundle\Entity\Client',
            'label' => 'Unternehmen',
        ));
    }
     $formMapper
        ->add('editor', 'sonata_type_model', array(
            'required' => false,
            'multiple' => false,
            'by_reference' => false,
            'class' => 'UserBundle\Entity\User',
            'label' => 'Bearbeiter',
        ))
    ;
}

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