简体   繁体   English

我的变量“表格”不存在 - Symfony 6

[英]My variable "form" does not exist - Symfony 6

I try to allow my user to add and update his profile picture.我尝试允许我的用户添加和更新他的个人资料图片。 So I created a new function in UserController.php file in order to retrieve the form on login.html.twig. So I created a new function in UserController.php file in order to retrieve the form on login.html.twig.

<?php

namespace App\Controller;

use App\Entity\User;
use App\Form\UserType;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class UserController extends AbstractController
{
    #[Route('add_photo', name: 'add_photo')]
    public function addPhoto(
        Request $request,
        UserRepository $userRepository,
        SluggerInterface $sluggerInterface,
        EntityManagerInterface $entityManagerInterface
    ){
        $connected = $this->getUser();
        $useremail = $connected->getUserIdentifier();
        $user = $userRepository->findOneBy(['email' => $useremail]);

        $userform = $this->createForm(UserType::class, $user);
        $userform->handleRequest($request);

        if ($userform->isSubmitted() && $userform->isValid()) {
            $imagefile = $userform->get('photo')->getData();

            if ($imagefile){
                $originalFileName = pathinfo($imagefile->getClientOriginalName(), PATHINFO_FILENAME);
                $safeFileName = $sluggerInterface->slug($originalFileName);
                $newFileName = $safeFileName . '-' . uniqid() . '.' . $imagefile->guessExtension();
    
                $imagefile->move(
                    $this->getParameter('images_directory'),
                    $newFileName
                );
    
                $user->setPhoto($newFileName);
            }

            $entityManagerInterface->persist($user);
            $entityManagerInterface->flush();
            return $this->redirectToRoute('login');
        }

        return $this->renderForm('security/login.html.twig', [
            'user' => $user,
            'form' => $userform,
        ]);
    }

When I go to the route that leads me to login.html.twig, I have this error: Variable "form" does not exist.当我 go 到引导我登录的路线时。html.twig,我有这个错误:变量“表单”不存在。

When you use a Form to submit data you need a way to tell the html that you're using a 'form'.当您使用表单提交数据时,您需要一种方法来告诉 html 您正在使用“表单”。 eg:例如:

 <FORM METHOD='POST' ACTION='http:veriget.php'>

You need a place to get the user data:您需要一个地方来获取用户数据:

 <INPUT TYPE='text' ID='Quantity' NAME='Quantity' SIZE='3' VALUE='1'>

Where ACTION is the page where the data is submitted to by using something like:其中 ACTION 是使用以下内容提交数据的页面:

 <input type='SUBMIT' name='menu' value='Place Order'>

With PHP I find its easier to use/construct a HTML page for the form.使用 PHP,我发现它更易于使用/构建表单的 HTML 页面。 Then submit to a PHP file.然后提交到 PHP 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM