Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom collection type displays "label empty" when adding new element #1399

Closed
laurent-bientz opened this issue Nov 21, 2016 · 8 comments
Closed

Comments

@laurent-bientz
Copy link
Contributor

laurent-bientz commented Nov 21, 2016

Hi,

I've implemented a custom collection type with my own form type binded on my Subdomain Entity:

Shop:
    class: ShopBundle\Entity\Shop
    new:
        fields:
            # ...
             - { property: 'subdomains', type: 'collection', type_options: { entry_type: 'PortalBundle\Form\Type\SubdomainType', by_reference: false },  label: 'Sous-domaines' }

In my SubdomainType, I just have two fields:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class)
        ->add('domain', EntityType::class, array('class' => 'ShopBundle\Entity\Domain'))
    ;
}

My relations between entities are like that: Shop have a OneToMany to Subdomain which have a ManyToOne to Domain

All works like a charm, entities are created, foreign keys are pretty good but the widget display the block "label empty" foreach Subdomain item:

adding_item


I also have the same problem with a simple ManyToOne to user Entity:

- { property: 'admin', type: 'PortalBundle\Form\Type\UserType', label: 'Email du webmaster' }

On my UserType, only a simple TextType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('email', TextType::class)
    ;
}

But it displays the same weird "empty label":

manytoone

All is working but any idea of what I'm doing wrong for the widget displaying?

Thanks


EDIT
If it can helps, I haven't these empty labels when reloading with validation fail:

reloading

@CruzyCruz
Copy link

I think it's the normal behavior (in a new view) of EasyAdmin when you embedd a form related to an entity . It tells that the property that defines the relationship (for example subdomains here) is null. The same for me in my application. Once your form is submitted and validated it disappears. I didn't realize that it could also disappear when validation fails.

Do you think that it should be preferable to avoid this label ?

@laurent-bientz
Copy link
Contributor Author

Hi @CruzyCruz

On the one hand, I think keeping the "empty" before adding new element is normal (same thing if we've deleted all the children), but on the other hand, when we've added a new element, for me (and for customer), it's weird to keep it, even if u'r right, it's not created yet.

I understand your explanation but why it disappears if validation fails? cause no persist yet and no relations are set ; so if it was a wanted state, why this difference between load and reload?

@javiereguiluz what's your point of view?

@CruzyCruz
Copy link

@laurent-bientz

I tried the following to understand why the label empty is displayed when validation fails:

I have a Restaurant entity with a OneToOne relationship with a Coordinates entity (an address). If I make the validation fails, the empty label disappears (like for you).

easy_admin:                              
        Restaurant:
            class : FBN\GuideBundle\Entity\Restaurant         
            form:
                fields:
                    - { property: 'coordinates', type: 'FBN\GuideBundle\Form\CoordinatesType', type_options: { required: true } } 

I introduced those two tests in my code:

  1. At Restaurant entity level, I dumped the underlying Coordinates object at setter level:
class Restaurant extends Article
{
    /**
     * @ORM\OneToOne(targetEntity="FBN\GuideBundle\Entity\Coordinates", inversedBy="restaurant", cascade={"persist","remove"})
     * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
     * @Assert\Valid()
     */
    private $coordinates;

    /**
     * Set coordinates.
     *
     * @param \FBN\GuideBundle\Entity\Coordinates $coordinates
     *
     * @return Restaurant
     */
    public function setCoordinates(\FBN\GuideBundle\Entity\Coordinates $coordinates)
    {
        //////////////////////////
        dump('TEST 1');
        dump($coordinates);
        die();
        //////////////////////////
        $this->coordinates = $coordinates;
        $coordinates->setRestaurant($this);

        return $this;
    }
}
  1. In my custom AdminController I introduced a form event to catch the form datas just before the form submission and before validation. See here.
class AdminController extends BaseAdminController
{
    public function createRestaurantEntityFormBuilder($entity, $view)
    {
        $formBuilder = parent::createEntityFormBuilder($entity, $view);

        //////////////////////////
        $formBuilder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
            $data = $event->getData();
            dump('TEST 2');
            dump($data->getCoordinates());
            die();
        }, 900);
        //////////////////////////

        // .....
    }
}

Results:

  • TEST 1 is displayed before TEST 2 : normal
  • Both tests gives a non null instance of Coordinates : consistent
TEST 1
Restaurant.php on line 325:
Coordinates {#2147 ▼
  -coordinatesCountry: CoordinatesCountry {#2220 ▶}
  -coordinatesFR: CoordinatesFR {#2138 ▶}
  -restaurant: null
  -winemakerDomain: null
  -shop: null
  -event: null
  -id: null
}
TEST 2 
AdminController.php on line 68:
Coordinates {#1839 ▼
  -coordinatesCountry: CoordinatesCountry {#1912 ▶}
  -coordinatesFR: CoordinatesFR {#1830 ▶}
  -restaurant: Restaurant {#1221 ▶}
  -winemakerDomain: null
  -shop: null
  -event: null
  -id: null
}

So I suppose that when the view is displayed with error messages, coordinates field of Restaurant entity is not null and the association is considered as not null.

@laurent-bientz
Copy link
Contributor Author

@CruzyCruz

Great catch, so now we know why no empty label are present when reloading with errors, even if the entity is not yet persisted, the relation exists.

Maybe @javiereguiluz can say to us if it's possible to remove the empty label directly in front-end while adding children or a custom entity?

@CruzyCruz
Copy link

CruzyCruz commented Nov 22, 2016

@laurent-bientz

Have a look at this template and this CSS file (.label and .label-empty).

In this part of the doc, even if it is about list, show and search view, it is said that:

In addition, there are other templates defined to render special labels:

label_empty.html.twig, used when the property to render is empty (it's used for arrays, collections, associations, images, etc.)

I don't know if this template is used (it seems) in edit view but it is the same HTML element <span class="label label-empty">.

Then have a look at the documentation related to advanced design configuration for edit and new view.

@laurent-bientz
Copy link
Contributor Author

@CruzyCruz

yep, I already saw that, I'll give it a try when I can but I'm pretty sure that it's the same css class when collection is empty... and in this case, it's a good point to have this info.

Cheers

@laurent-bientz
Copy link
Contributor Author

@CruzyCruz

I fixed it with a simple css override:

easy_admin:
    design:
        assets:
            css:
                - '/assets/portal/css/easyadmin-tuning.css'
.field-user .collection-empty,
#shop_subdomains .collection-empty{
    display:none;
}

I know it's crappy but it's quick.

@javiereguiluz
Copy link
Collaborator

I'm closing this issue because we're starting a new phase in the history of this bundle (see #2059). We've moved it into a new GitHub organization and we need to start from scratch: no past issues, no pending pull requests, etc.

I understand if you are angry or disappointed by this, but we really need to "reset" everything in order to reignite the development of this bundle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants