Symfony 5 模态实体部分属性的形式 [英] Forms for partial attributes of entity in modal Symfony 5

查看:24
本文介绍了Symfony 5 模态实体部分属性的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中我的用户表包含字段:

I have a project where my user table has the fields:

id
username
email
password
mother_language
description
image

我使用:usernameemailpassword 进行注册.

I use : username, email, password, for registration.

注册后,我重定向到一个个人资料页面,在那里我有一个只有 mother_languagedesciption 的模式.另一种模式只允许修改同一用户的图像.

After signing up I redirect to a profile page where I have a modal that has only mother_language and desciption. Another modal allows only the modification of the image of the same user.

我的问题是:

  • 如何只更新单独"?使用表单类型的实体的属性.
  • 由于我正在尝试制作单页"WebApp,是否可以将我的所有表单呈现到配置文件模板中?('在一条路线上')
  • 我正在尝试做的最佳工作流程是什么?如果有帮助,我的用户控制器:
  • How can i update only "separate" attributes of an entity using a Form Type.
  • Since I am trying to make a "Single Page" WebApp, Is It possible to render all of my forms into the profile template? ('On a single route')
  • what's the optimal workflow for what i'm trying to do? My User Controller if it helps:
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Repositry\UserRepository;

class UserController extends AbstractController
{
    /**
     * @Route("/profile", name="profile", methods={"GET"})
     */
    public function index(): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
        $user = $this->getUser();
        return $this->render('profile.html.twig', [
            'controller_name' => 'UserController',      // I tried returning forms in this array but got errors
        ]);
    }

    /**
     * @Route("/profile", name="update", methods={"PUT"})
     * @param Request
     */
    public function updateUser(Request $request) : Response
    {
        $user = $this->getUser()->getId();
        return dump($user);
    }
}

推荐答案

我的一个项目遇到了类似的情况.您不需要加载多个表单.最简单的方法是创建一个单独的表单类型,其中只包含您需要更新的字段.

I am having a similar scenario for one of my projects. You don't need to load multiple forms. The easiest way is to create a separate form type with only the fields you need to update.

我的控制器如下所示:

public function edit(Request $request): Response
{
    $user = $this->getUser();
    $form = $this->createForm(UserType::class, $user);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $this->getDoctrine()->getManager()->flush();

        return $this->redirectToRoute('dashboard');
    }

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

和用户类型:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('email')
        ->add('name')
        ->add('university')
        ->add('year')
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => User::class,
    ]);
}

没有密码和其他注册时使用的东西.

Without password and other stuff used on registration.

注意:我正在编辑当前用户的信息.

Note: I am editing the information of the current user.

这篇关于Symfony 5 模态实体部分属性的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆