Symfony表单总是给我"This value should not be blank."消息



Symfony制作的一个简单登录表单遇到问题。这是控制器代码:

public function loginAction(Request $request){
        $user = new User();
        $form = $this->createFormBuilder($user)
        ->add('userID', 'text', array('label' => 'UserID'))
        ->add('password', 'password', array('label' => 'Password'))
        ->add('ricorda', 'checkbox', array(
                "required" => false,
                "mapped" => false
        ))
        ->add('login', 'submit')->getForm();
        $form->handleRequest($request);
        Debug::dump($_POST$user);
        /*if($form->isValid()){
        }*/
        return $this->render('TDDumbFEBundle:Default:auth.html.twig', array('form' => $form->createView()));
    }

这里是实体类用户,应该在其中存储并验证表单数据:

class User{
    private $userID;
    private $password;
    public function getUserID(){
        return $this->userID;
    }
    public function getPassword(){
        return $this->password;
    }
    public function setUserID($userID){
        $this->$userID = $userID;
    }
    public function setPassword($password){
        $this->$password = $password;
    }
}

最后,约束条件:

properties:
  userID:
    - NotBlank: ~
  password:
    - NotBlank: ~

嗯,不知道为什么,但无论我在userID和密码输入字段中键入什么,我都会得到一个

This value should not be blank.

消息。如果我对用户对象使用Debug::dump()方法,我会发现所有属性都是空的。

有什么想法吗?

简单的打字错误。

public function setUserID($userID){
    $this->$userID = $userID;          // *** Change to $this->userID = $userID
}
public function setPassword($password){
    $this->$password = $password;      // *** Change to $this->password = $password;
}

相关内容

最新更新