php - Symfony 4 表单验证在尝试更新用户实体时返回 "This value should not be blank."

标签 php symfony

我在尝试更新 Symfony 4 中的用户实体时遇到问题。

我创建了一个UserAccountLoginType表单,用户可以在其中更改他们的电子邮件用户名,但是当我尝试提交此表单时使用新用户名/电子邮件填写表单没有任何反应。我确信这两个字段是正确且唯一的。在 var_dumping $form->getErrors() 之后,我总是收到错误:

This value should not be blank.

没有任何信息是哪个字段导致此错误。

App\Entity\User.php:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\ORM\Id\AbstractIdGenerator;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @UniqueEntity(fields="email", message="Email already taken")
 * @UniqueEntity(fields="username", message="Username already taken")
 * @ORM\HasLifecycleCallbacks
 */
class User implements UserInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="App\Utils\CustomGenerator")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     * @Assert\NotBlank()
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=2, nullable=true)
     */
    private $language;

    /**
     * @Assert\NotBlank()
     * @Assert\Length(max=4096)
     */
    private $plainPassword;

    /**
     * The below length depends on the "algorithm" you use for encoding
     * the password, but this works well with bcrypt.
     *
     * @ORM\Column(type="string", length=64)
     */
    private $password;

    /**
     * @ORM\Column(type="datetime")
     * @Assert\DateTime()
     */
    private $createdAt;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\UserProfile", mappedBy="user")
     */
    private $profile;

    /**
     * @ORM\Column(type="simple_array", nullable=true)
     */
    private $roles;

App\Form\UserAccountLoginType.php:

class UserAccountLoginType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username', TextType::class)
            ->add('email', TextType::class)
            ->add('save', SubmitType::class, array('label' => 'Zapisz'))
        ;
    }

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

App\Controller\UserController.php:

/**
 * @Route("/system/user/account/login", name="user_account_login_mail")
 */
public function accountLoginMail(Request $request) {

    $user = $this->getUser();

    if (!$user) {
        throw $this->createNotFoundException(
            'Nie znaleziono użytkownika.'
        );
    }

    $form = $this->createForm(UserAccountLoginType::class, $user);
    $form->handleRequest($request);

    if ($form->isSubmitted()) {
        $em = $this->getDoctrine()->getManager();

        if ($form->isValid()) {

            $em->persist($user);
            $em->flush();

            $this->addFlash(
                'info',
                'Zapisano ustawienia!'
            );

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

        $em->refresh($user);
    }

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

有人知道如何解决这个问题吗? 感谢您的帮助。

最佳答案

问题解决了!我已打开 Profiler,现在可以看到此问题的根源。

Caused by:
    ConstraintViolation {#4406 ▼
        root: Form {#2794 …}
        path: "data.plainPassword"
        value: null
}

问题是我没有附加对用户实体进行更改所需的 plainPassword 字段。

关于php - Symfony 4 表单验证在尝试更新用户实体时返回 "This value should not be blank.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48813643/

相关文章:

php - 使用来自自定义服务的编译器传递加载 Symfony 的参数

Symfony2访问服务中的用户和原则

php - 无法使用 PDO 将文件名插入到 sql 中

php - 属性作为数组

php - 如何在开始编写代码之前规划我的基于 Web 的项目?

Symfony 4针对不同环境的不同安全配置

测试控制台命令时的 Symfony 模拟服务

php - 如何使用键从数组中删除元素

php - 尽管权限正确,Web PHP 实例仍无法运行可执行文件

json - Symfony2 - 检查接受 HTTP header 是否支持 "application/json"