表单中的一个字段在 Symfony3 中未验证
One field in form doesn't validate in Symfony3
问题是 "confirmPassword" 从未通过验证。
是的,还有所有的getter和setter。
"test" 和 "password" 变量验证正常。
我看不出这些变量之间的区别,除了 test 是文本类型,而 confirmPassword 是 password 类型。
消息是 "This value should not be blank" 即使我在该字段中输入文本也是如此。
这里有什么问题?
////////////////////用户实体class(部分):
/**
* @ORM\Column(type="string", length=128)
* @Assert\NotBlank (groups={"registration"})
*/
private $password;
/**
* @Assert\NotBlank(
* groups={"registration"},
* )
*/
private $confirmPassword;
/**
* @Assert\NotBlank(
* groups={"registration"},
*/
private $test;
///////////////////////////////用户类型 class:
class Register extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => array('registration'),
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username',TextType::class, array('error_bubbling'=>false,'label'=>'Login'))
->add('email', TextType::class, array('error_bubbling'=>false, 'label' => 'E-mail'))
->add('password',PasswordType::class, array('error_bubbling'=>false,'label' => 'password'))
->add('confirmPassword',PasswordType::class, array('error_bubbling'=>false,'label' => 'password confimation'))
->add('test',TextType::class, array('error_bubbling'=>false,'label' => 'test'))
->add('save', SubmitType::class)
;
}
}
///////////////// 最后,提交函数:
public function registerAction(Request $request, $done = 0)
{
$user = new \AppBundle\Entity\User();
$form = $this->createForm(Register::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$validator = $this->get('validator');
if($form->isValid()){
return $this->redirectToRoute('user_register',array('done' => 1));
}
}
return $this->render('front/users/register.html.twig', [
'form'=>$form->createView(),
'done'=>$done,
'errors' => array()
]);
}
编辑:添加了 getter 和 setter。请注意出现了垂直滚动条。
///////////getter 和 setter
public function getUsername() {
return $this->username;
}
public function getSalt() {
// you *may* need a real salt depending on your encoder
// see section on salt below
return $this->salt;
}
public function getPassword() {
return $this->password;
}
public function getRoles() {
$ret_val = array();
$roles = $this->getUserRoles();
if($roles) {
foreach($roles as $Role) {
$ret_val[] = $Role->getRoleName();
}
}
return $ret_val;
}
public function eraseCredentials() {
}
public function getConfirmPassword() {
return $this->password;
}
public function setConfirmPassword($password) {
$this->confirmPassword = $password;
return $this;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password) {
$this->password = $password;
return $this;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email) {
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail() {
return $this->email;
}
public function setTest($test){
$this->test = $test;
}
public function getTest(){
return $this->test;
}
您的 getConfirmPassword()
方法没有 return 预期的 属性(它 return 是 $password
)。
问题是 "confirmPassword" 从未通过验证。 是的,还有所有的getter和setter。
"test" 和 "password" 变量验证正常。
我看不出这些变量之间的区别,除了 test 是文本类型,而 confirmPassword 是 password 类型。
消息是 "This value should not be blank" 即使我在该字段中输入文本也是如此。
这里有什么问题?
////////////////////用户实体class(部分):
/**
* @ORM\Column(type="string", length=128)
* @Assert\NotBlank (groups={"registration"})
*/
private $password;
/**
* @Assert\NotBlank(
* groups={"registration"},
* )
*/
private $confirmPassword;
/**
* @Assert\NotBlank(
* groups={"registration"},
*/
private $test;
///////////////////////////////用户类型 class:
class Register extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => array('registration'),
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username',TextType::class, array('error_bubbling'=>false,'label'=>'Login'))
->add('email', TextType::class, array('error_bubbling'=>false, 'label' => 'E-mail'))
->add('password',PasswordType::class, array('error_bubbling'=>false,'label' => 'password'))
->add('confirmPassword',PasswordType::class, array('error_bubbling'=>false,'label' => 'password confimation'))
->add('test',TextType::class, array('error_bubbling'=>false,'label' => 'test'))
->add('save', SubmitType::class)
;
}
}
///////////////// 最后,提交函数:
public function registerAction(Request $request, $done = 0)
{
$user = new \AppBundle\Entity\User();
$form = $this->createForm(Register::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$validator = $this->get('validator');
if($form->isValid()){
return $this->redirectToRoute('user_register',array('done' => 1));
}
}
return $this->render('front/users/register.html.twig', [
'form'=>$form->createView(),
'done'=>$done,
'errors' => array()
]);
}
编辑:添加了 getter 和 setter。请注意出现了垂直滚动条。
///////////getter 和 setter
public function getUsername() {
return $this->username;
}
public function getSalt() {
// you *may* need a real salt depending on your encoder
// see section on salt below
return $this->salt;
}
public function getPassword() {
return $this->password;
}
public function getRoles() {
$ret_val = array();
$roles = $this->getUserRoles();
if($roles) {
foreach($roles as $Role) {
$ret_val[] = $Role->getRoleName();
}
}
return $ret_val;
}
public function eraseCredentials() {
}
public function getConfirmPassword() {
return $this->password;
}
public function setConfirmPassword($password) {
$this->confirmPassword = $password;
return $this;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password) {
$this->password = $password;
return $this;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email) {
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail() {
return $this->email;
}
public function setTest($test){
$this->test = $test;
}
public function getTest(){
return $this->test;
}
您的 getConfirmPassword()
方法没有 return 预期的 属性(它 return 是 $password
)。