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 post 到 register
来执行注册时,我收到了两次以下错误消息:
Please enter your name.
你知道为什么会这样吗?我会解决它吗?
编辑 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;
}
}
如果您的字段不为空(表单提交),您应该检查您是否在您的实体中为 name
属性 就像下面的例子(使用注解):
@Assert\NotNull
@Assert\NotBlank
.
我已经扩展了 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 post 到 register
来执行注册时,我收到了两次以下错误消息:
Please enter your name.
你知道为什么会这样吗?我会解决它吗?
编辑 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;
}
}
如果您的字段不为空(表单提交),您应该检查您是否在您的实体中为 name
属性 就像下面的例子(使用注解):
@Assert\NotNull
@Assert\NotBlank
.