I make a library app, you can set a book image, and my form look as:
The yellow discord image is the actual image of my Book entity.
If I upload a file I cannot see it (label trick to upload, input hidden so).
It would be perfect if the image could change JUST WHEN i upload an image.
Example, I'm uploading blue discord image (example), so the image change right now on the form, and it will update Only if I submit the form).
How can I do that ?
My controller:
/**
 * @Route("/{id}/edit", name="book_edit", methods={"GET","POST"})
 */
public function edit(Request $request, Book $book, BookRepository $bookRepository, ClientRepository $clientRepository): Response
{
    $form = $this->createForm(BookType::class, $book);
    $image_source = $book->getImage()->getSrc();
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $image = new Image();
        if($request->files->get('book')['image']['src'] !== null && $request->files->get('book')['image']['src'] !== 'no_change')
        {
            $file = $request->files->get('book')['image']['src'];
            $name = $file->getClientOriginalName('src');
            $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
            $alt = $book->getImage()->getText();
            $image->setSrc($fileName);
            $image->setText($alt);
            $book->setImage($image);
            $fileSystem = new Filesystem();
            try {
                $fileSystem->remove(array('symlink', $this->getParameter('Cover_directory'), $image_source));
            } catch (IOExceptionInterface $exception) {
                echo "An error occurred while creating your directory at ".$exception->getPath();
            }
            try {
                $file->move($this->getParameter('Cover_directory'), $fileName);
            } catch(FileException $e) { 
                // die($e); 
            }
        }
        else {
            $image = $book->getImage();
            $image->setSrc($image_source);
            $image->setText($image->getText());
            $book->setImage($image);
        }
        if($request->request->get('client'))
        {
            $client = $clientRepository->find($request->request->get('client'));
            $book->setClient($client);
        }
        $this->getDoctrine()->getManager()->persist($image);
        $this->getDoctrine()->getManager()->persist($book);
        $this->getDoctrine()->getManager()->flush();
        return $this->redirectToRoute('book_index', ['id' => $book->getId()]);
    }
    $clients = $clientRepository->findAll();
    // dump($clients);die;
    return $this->render('book/edit.html.twig', [
        'book' => $book,
        'form' => $form->createView(),
        'clients' => $clients,
    ]);
}

 
    