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

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

Open
dwd-akira opened this issue Apr 23, 2024 · 4 comments · May be fixed by #6280
Open

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

dwd-akira opened this issue Apr 23, 2024 · 4 comments · May be fixed by #6280

Comments

@dwd-akira
Copy link
Contributor

dwd-akira commented Apr 23, 2024

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
Copy link
Contributor

zorn-v commented Apr 24, 2024

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

zorn-v added a commit to zorn-v/EasyAdminBundle that referenced this issue Apr 24, 2024
@zorn-v zorn-v linked a pull request Apr 24, 2024 that will close this issue
@dwd-akira
Copy link
Contributor Author

dwd-akira commented Apr 24, 2024

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
Copy link
Contributor

zorn-v commented Apr 24, 2024

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
Copy link
Contributor Author

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

Yes it's good.

Thanks for ->setEntryIsComplex()

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

Successfully merging a pull request may close this issue.

2 participants