Я хочу редактировать информацию о пользователе. Когда я отправляю форму редактирования, изображение профиля пользователя не изменяется, но другая информация изменяется. Я использую symfony 5.0 версию.
UserController. php
/**
* @Route("/{id}/edit", name="user_edit", methods={"GET","POST"})
*/
public function edit(Request $request, $id, User $user): Response
{
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted()) {
/** @var UploadedFile $imageFile **/
$file = $form->get('image')->getData();
if ($file) {
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
try {
$file->move(
$this->getParameter('images_directory'),
$newFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
//$user->setImage($newFilename);
$user->setImage(
new File($this->getParameter('images_directory').'/'.$user->getImage())
);
}
/** file upload end **/
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('user_profile', ['id'=>$id]);
}
return $this->render('user/edit.html.twig', [
'user' => $user,
'form' => $form->createView(),
]);
}
UserType. php
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('surname')
->add('birthday', DateType::class, [
'widget' => 'single_text',
])
->add('image', FileType::class, [
'mapped'=>false,
'required'=> false,
'constraints'=> [
new File([
'maxSize'=>'5000k',
'mimeTypes'=> [
'image/*',
],
'mimeTypesMessage'=>'Please upload a valid Image File',
])
],
])
->add('gender')
->add('country')
->add('biography')
->add('facebook')
->add('twitter')
->add('instagram')
->add('linkedin')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}