EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
3.99k stars 1.01k forks source link

PR #6200 : Unsupported operand types: string + int #6277

Open dwd-akira opened 3 weeks ago

dwd-akira commented 3 weeks ago

Hi @zorn-v,

I have problem when updating EasyAdmin 4.9.4 to 4.9.5 or higher.

6200 Fix empty collection first item index (zorn-v) create this error for me :

Unsupported operand types: string + int

The problem appears with TranslatableInterface

In src/Resources/views/crud/form_theme.html.twig (line 140): 'data-num-items': form.children is empty ? 0 : max(form.children|keys) + 1, generate the error

The "EntityTranslation" has no ID, just locale and translation. {{ dump(max(form.children|keys)) }} return fr

zorn-v commented 3 weeks ago

max(form.children|keys) was strange to count num items in first place, but I left it as is. Can you show how your collection is looks like ? Probably you need other field/form type

dwd-akira commented 3 weeks ago

I use knplabs/doctrine-behaviors for entity translation https://github.com/KnpLabs/DoctrineBehaviors/blob/master/docs/translatable.md

Example :

namespace App\Entity;

use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;

class Product implements TranslatableInterface
{
    use TranslatableTrait;

    private $id;
    ...
}
namespace App\Entity;

use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;

class ProductTranslation implements TranslationInterface
{
    use TranslationTrait;

    private $id;

    private $label; // the translation for the locale
    ...
}

use TranslatableTrait; add a collection of ProductTranslation in Product

use TranslationTrait; add locale field in ProductTranslation

In my ProductCrudController

yield CollectionField::new('translations')
    ->setFormTypeOptions([
        'label' => false,
        'entry_type' => TranslationType::class,
        'entry_options' => [
            'data_class' => ProductTranslation::class,
        ],
    ]);

In my TranslationType

class TranslationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('label', TextType::class, [
                'required' => true,
                'constraints' => [new NotBlank(), new Length(['max' => 255])],
            ])
            ->add('locale', LocaleType::class, [
                'required' => true,
                'constraints' => [new NotBlank(), new Length(['max' => 5])],
                'preferred_choices' => ['fr', 'en'],
            ]);
    }
}

I tested without TranslationType with ProductTranslationCrudController and useEntryCrud; i have the same problem.

For the moment, I revert PR #6200, I have no error but certainly problem with empty collection first item index.

But max(form.children|keys) return a string (the locale fr or en)

What do you think to use length in form_theme ?

Edit: I saw your new PR with length ^^ I tested and no error

zorn-v commented 3 weeks ago

Check constraint violations for subitems. For example label with length > 255 (or temporary decrease max). Is it shown in proper place ?

BTW, you need to add ->setEntryIsComplex() for CollectionField to proper highlight invalid entry with red border, as invalid class selector is .field-collection-item.field-collection-item-complex.is-invalid which is not proper as I think, but as is.

You also need to add Assert\Valid for translations field for validate subitems.

dwd-akira commented 3 weeks ago

Check constraint violations for subitems. Is it shown in proper place ?

Yes it's good.

Thanks for ->setEntryIsComplex()