簡體   English   中英

Symfony FosUserBundle:自定義用戶配置文件表格使用戶注冊不起作用

[英]Symfony FosUserBundle: Custom User Profile Form makes user registration not work

我已經擴展了ProfileFormType表單以更新現有的用戶配置文件:

namespace AppUserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class UserProfileFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name',TextType::class,array('label'=>'profile.first_name','required' => false));
        $builder->add('surname',TextType::class,array('label'=>'profile.surnname','required' => false));
        $builder->add('email',TextType::class,['required' => false]);
        $builder->add('description',TextareaType::class,['required' => false]);

    }


    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\ProfileFormType';
    }

    public function getBlockPrefix()
    {
        return 'app_user_profile';
    }

    // For Symfony 2.x
    public function getName()
    {
        return $this->getBlockPrefix();
    }
}

但是由於某種原因,當我通過執行http帖子進行registerregister兩次出現以下錯誤消息:

請輸入你的名字。

您是否知道為什么會發生這種情況,我將如何解決?

編輯1

我的實體是:

namespace AppUserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="user_image", type="string", length=255, nullable=true)
     */
    private $userImage;

    /**
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    private $name;

    /**
     * @ORM\Column(name="surname", type="string", length=255, nullable=true)
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The surname is too short.",
     *     maxMessage="The surname is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    private $surname;

    /**
     * @ORM\Column(name="description", type="string", length=1024, nullable=true)
     */
    private $description;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    public function setUserImage($imagepath)
    {
        $this->userImage=$imagepath;
    }

    public function functiongetUserImage()
    {
        return $this->userImage;
    }

    public function setName($name)
    {
        $this->name=strip_tags($name);
    }

    public function getName()
    {
        return $this->name;
    }

    public function setSurname($surname)
    {
        $this->surname=strip_tags($surname);
    }

    public function getSurname()
    {
        return $this->surname;
    }

    public function setDescription($description)
    {
        $this->description=$description;
    }

    public function getDescription()
    {
        return $this->description;
    }
}

如果您的字段不為空( 在表單Submit上 ),則應檢查在Entity中是否已對name屬性分配了一些約束,如下面的示例( 使用注解 ):

  • @Assert\\NotNull
  • @Assert\\NotBlank

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM