Symfony形式总是给我“这个值不应该是空白的”。信息

时间:2013-08-31 11:08:53

标签: forms validation symfony

我在使用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()方法,我发现所有属性都是空的。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

简单的拼写错误。

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

public function setPassword($password){
    $this->$password = $password;      // *** Change to $this->password = $password;
}
相关问题