4

I get this error message: Expected argument of type "App\Entity\Artist or null", "string" given at property path "artist".

This is the code of my form in symfony

$builder
        ->add('date', DateType::class, [
            'widget' => 'single_text'
        ])
        ->add('artist', TextType::class)
        ->add('City',TextType::class)
        ;

And this is the template:

{{ form_start(form)}}
    {{ form_widget(form)}}
        <button class="btn btn-secondary">{{ button|default('Enregistrer')}}</button>
{{ form_end(form)}}

Entity

class Artist
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;
/**
 * @ORM\Column(type="string", length=45)
 */
private $name;
/**
 * @ORM\OneToMany(targetEntity="App\Entity\Event", mappedBy="artist", cascade={"persist","remove"})
 */
private $events;
public function __construct()
{
    $this->events = new ArrayCollection();
}
public function getId(): ?int
{
    return $this->id;
}
public function getName(): ?string
{
    return $this->name;
}
public function setName(?string $name): self
{
    $this->name = $name;
    return $this;
}
/**
 * @return Collection|Event[]
 */
public function getEvents(): Collection
{
    return $this->events;
}
public function addEvent(Event $event): self
{
    if (!$this->events->contains($event)) {
        $this->events[] = $event;
        $event->setArtist($this);
    }
    return $this;
}
public function removeEvent(Event $event): self
{
    if ($this->events->contains($event)) {
        $this->events->removeElement($event);
        // set the owning side to null (unless already changed)
        if ($event->getArtist() === $this) {
            $event->setArtist(null);
        }
    }
    return $this;
}
public function __toString()
{
    return $this->name;
}
}

This is the error :

Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: "Expected argument of type "App\Entity\Artist or null", "string" given at property path "artist"." at D:\wamp64\www\app_music_events\vendor\symfony\property-access\PropertyAccessor.php line 173

{
    "exception": {}
}

This is the controller

/**
 * Method for editing an event
 *
 * @Route("/admin/edit/{id}", name="admin.edit", methods="GET|POST")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function edit(Event $event, Request $request): Response
{
    $form = $this->createForm(EventType::class, $event);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $this->em->flush();
        $this->addFlash('success', 'Le concert a été modifié avec succès !');
        return $this->redirectToRoute('admin.index');
    }

    return $this->render('admin/edit.html.twig', [
            'event' => $event,
            'form' => $form->createView()
        ]
    );
}

I don't understand. I do not have this problem when I do not specify Textype in the form.

What is the problem? Thanks

3
  • Can you share the code from your controller by editing your question? Commented Jan 1, 2019 at 17:37
  • Hello, I added the controller code above. Thanks Commented Jan 1, 2019 at 18:06
  • you are adding artist fieldType as TextType::class Commented Jan 1, 2019 at 18:32

1 Answer 1

3

The problem is that you need to persist an Artist entity along with your Event. Symfony expects an Artist entity type and not a string. Try with EntityType::class instead of TextType::class

->add('artist', EntityType::class, [
   'class' => Artist::class
]);

For more information on the EntityType field, please check the documentation

Sign up to request clarification or add additional context in comments.

5 Comments

I have an error : The required option "class" is missing. Uncaught PHP Exception Symfony\Component\OptionsResolver\Exception\MissingOptionsException: "The required option "class" is missing." at D:\wamp64\www\app_music_events\vendor\symfony\options-resolver\OptionsResolver.php line 763 { "exception": {} }
I have new problem. It is a crud. Since we replaced texttype with entitytype, fields can not be changed How to do?
By modifying textype by entitytype, the field can not be changed. The initial problem is not solved by this way. Another idea of the problem?
Can you please elaborate? I want to help you but your problem is not clear. I would post a new question in this regard, with the code where you wish to edit your entity type
I posted a new question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.