0

这是我的代码:

  $form = $this->createFormBuilder($signupAttempt)
     ->add('email', 'text', array("label" => "your email:"))
     ->add('password', 'password', array("label" => "your password:"))
     ->add('passwordRepeat', 'password', array("label" => "repeat password:"))
     ->getForm();


  if ($request->isMethod('POST')) {
     $form->bindRequest($request);
     $attempt = $form->getData();
     $this->changeSomeAttributesOfSignupAttempt($attempt); // this does not work
     if ($form->isValid()) { // this is not taking into account the modification made inside changeSomeAttributesOfSignupAttempt
        return new Response("data provided are valid - u signiged up!");
     }
  }

看到我的问题了吗?我想对实体进行一些更改,并希望表单能够意识到这些更改。不幸的是,我所做的更改似乎没有被察觉,因此,validaition.xml 中为类 SignupAttempt 定义的规则没有得到满足。

这是实体SignupAttempt的validation.xml:

  <getter property="emailInUseAlready">
     <constraint name="False">
        <option name="message">signup_attempt.whole.email_in_use</option>
     </constraint>
  </getter>

和实体类本身:

class SignupAttempt {

   protected $id = null;
   protected $email = null;
   protected $password = null;
   protected $passwordRepeat = null;
   protected $emailInUseAlredy = true;

   public function __construct($email = null, $password = null, $passwordReapeat = null) {
      $this->email = $email;
      $this->password = $password;
      $this->passwordRepeat = $passwordReapeat;
   }

   public function getId() {
      return $this->id;
   }

   public function setId($id) {
      $this->id = $id;
   }

   public function getEmail() {
      return $this->email;
   }

   public function setEmail($email) {
      $this->email = $email;
   }

   public function getPassword() {
      return $this->password;
   }

   public function setPassword($password) {
      $this->password = $password;
   }

   public function getPasswordRepeat() {
      return $this->passwordRepeat;
   }

   public function setPasswordRepeat($passwordRepeat) {
      $this->passwordRepeat = $passwordRepeat;
   }

   public function setEmailInUseAlready($bool) {
      $this->emailInUseAlredy = $bool;
   }

   public function isEmailInUseAlready() {
      return $this->emailInUseAlredy;
   }

   public function isSecondPasswordMatching() {
      return $this->password === $this->passwordRepeat;
   }

   public function import(array $data) {
      throw new \RuntimeException("implement this");
   }
}

任何的想法?

4

1 回答 1

0

当执行$form->isValid()时,返回的(布尔)值实际上是在请求绑定到表单时预先评估的。

因此,更改返回的实体的值$form->getData()是完全没有用的,因为验证是预先发生的,并且是在实体对象最初创建时所持有的初始值上进行的。

于 2012-12-08T14:16:02.577 回答