[英]One field in form doesn't validate in Symfony3
问题是“ confirmPassword”从不通过验证。 是的,也有所有的获取器和设置器。
“ test”和“ password”变量验证OK。
我看不到这些变量之间的区别,只不过测试的类型是文本,如果类型是密码,则是ConfirmPassword。
即使我确实在此字段中输入文本,也会出现消息“此值不能为空”。
怎么了
////////////////////用户实体类(的一部分):
/**
* @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 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()
方法不返回预期的属性(它返回$password
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.