Bonjour à tous,
Lors d'une édition d"une recette, j'ai cette erreur :
Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue(): Argument #1 ($object) must be of type ?object, string given, called in /home/julien/Documents/Dev/Newdevcreator/cook-book/vendor/symfony/form/ChoiceList/ArrayChoiceList.php on line 181
RecipeType
<?php
namespace App\Form;
use App\Entity\Recipe;
use App\Entity\Category;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Vich\UploaderBundle\Form\Type\VichImageType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
class RecipeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('ingredient', CollectionType::class, [
'entry_type' => IngredientType::class,
'label' => 'Ingrédients',
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
])
->add('recipeStep', CollectionType::class, [
'entry_type' => RecipeStepType::class,
'label' => 'Etapes',
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
])
->add('title', TextType::class, [
'attr' => [
'placeholder' => 'Exemple : Poulet au citron',
'autocomplete' => 'off',
],
'label' => 'Titre de la recette',
'label_attr' => [
'class' => 'is-input-required'
],
'required' => true,
])
->add('difficulty', ChoiceType::class, [
'label' => 'Difficulté de la recette',
'label_attr' => [
'class' => 'is-input-required'
],
'choices' => [
'Simplissime' => Recipe::SIMPLE,
'Facile' => Recipe::EASY,
'Moyenne' => Recipe::AVERAGE,
'Difficile' => Recipe::DIFFICULT,
'Niveau grand chef' => Recipe::LEVEL_GRAND_CHEF
]
])
->add('personCount', IntegerType::class, [
'attr' => [
'placeholder' => 'Exemple : 4',
'autocomplete' => 'off',
],
'label' => 'Pour combien de personnes ?',
'label_attr' => [
'class' => 'is-input-required'
],
'required' => true,
])
->add('timeCount', IntegerType::class, [
'attr' => [
'placeholder' => 'Exemple : 4',
'autocomplete' => 'off',
],
'label' => 'Temps total de la recette (en minutes)',
'label_attr' => [
'class' => 'is-input-required'
],
'required' => true,
])
->add('imageFile', VichImageType::class, [
'label' => 'Image de la recette',
'label_attr' => [
'class' => 'is-input-required'
],
'attr' => [
'class' => 'filepond'
],
'help' => 'Formats acceptés : jpg, png. Taille maximale : 5Mo',
'required' => false,
'allow_delete' => false,
'download_uri' => false,
'image_uri' => false,
'asset_helper' => false,
'constraints' => [
new Image([
'maxSize' => '5M',
'mimeTypes' => [
'image/jpeg', 'image/png', 'image/jpg'
],
'mimeTypesMessage' => 'Veuillez télécharger un document image valide (jpg, jpeg, png)',
])
],
])
->add('categories', CategoryAutocompleteField::class, [
'label' => 'Catégorie de la recette',
'label_attr' => [
'class' => 'is-input-required'
],
'required' => true,
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Recipe::class,
]);
}
}
CategoryAutocompleteField
$resolver->setDefaults([
'class' => Category::class,
'placeholder' => 'Choisissez une ou plusieurs categories',
'choice_label' => 'title',
'label' => 'Choix des catégories',
'multiple' => true,
'required' => false,
'autocomplete' => true,
'attr' => [
'class' => 'min-widt'
],
'query_builder' => function (CategoryRepository $categoryRepository) {
return $categoryRepository->createQueryBuilder('categories');
},
// 'security' => 'ROLE_SOMETHING',
]);
IngredientType
$builder
->add('name', IngredientAutocompleteField::class, [
'label' => 'Ingrédient',
'label_attr' => [
'class' => 'is-input-required'
],
'required' => true,
'multiple' => false,
])
->add('measurement', MeasurementType::class, [
'mapped' => false,
'label' => false,
]);
IngredientAutocompleteField
$resolver->setDefaults([
'class' => Ingredient::class,
'placeholder' => 'Choose a Ingredient',
'choice_label' => 'name',
'label' => 'Choix des ingrédients',
'multiple' => true,
'required' => false,
'autocomplete' => true,
'attr' => [
'class' => 'min-widt'
],
'query_builder' => function (IngredientRepository $ingredientRepository) {
return $ingredientRepository->createQueryBuilder('ingredient');
},
// 'security' => 'ROLE_SOMETHING',
]);
MeasurementType
$builder
->add('quantity', IntegerType::class, [
'label' => 'Quantité',
'label_attr' => [
'class' => 'is-input-required'
],
])
->add('unity', ChoiceType::class, [
'label' => 'Unité',
'label_attr' => [
'class' => 'is-input-required'
],
'choices' => [
'Pièce(s)' => Measurement::PIECE,
'ml' => Measurement::LITER,
'g' => Measurement::MASS
]
]);
Ce que je veux
Il semblerait que le collectionType ou le autocompleteField ne renvoient pas l'objet, mais une chaine de caractères.
Je ne vois rien dans la doc à ce sujet.
si j'enlève "ingredient", "recipeStep" et "categories", mon formulaire marche.