Symfony 4表单验证返回"此值不应为空白。"在尝试更新用户实体时

时间:2018-02-15 18:04:08

标签: php symfony

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

我已经创建了一个UserAccountLoginType表单,用户可以在其中更改e-mailusername,但是当我尝试使用新用户名/ e-提交此表单时邮件什么都没发生我确信这两个字段是正确和独特的。 var_dumping $form->getErrors()之后我总是收到错误:

  

此值不应为空白。

没有信息哪个字段会导致此错误。

应用\实体\ 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;

应用\表格\ 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,
        ));
    }
}

应用\控制器\ 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
    ));
}

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

2 个答案:

答案 0 :(得分:1)

问题解决了!我打开了一个Profiler,现在我可以看到这个问题的来源。

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

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

答案 1 :(得分:0)

尝试将以下内容放入twig文件中:

{{form_errors(formulario)}}

或做var_dump(formulario-> getErrors(true));参数为true。

在这里你会得到错误。