php - Symfony2 ignores form validation of collection -
i have problem symfony2 , validation of email addresses collection.
entity\user
/** * user * * @orm\table() * @orm\entity */ class user { /** * @orm\id * @orm\column(type="integer") * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @var email[] * @orm\onetomany(targetentity="email", mappedby="user", cascade={"all"}, orphanremoval=true) * @assert\valid */ private $emails; public function __construct() { $this->emails = new \doctrine\common\collections\arraycollection(); } public function getid() { return $this->id; } /** * add emails * * @param email $emails * @return user */ public function addemail(email $emails) { if($emails->getemail() !== null && strlen($emails->getemail()) > 0) { $emails->setuser($this); $this->emails[] = $emails; } return $this; } /** * remove emails * * @param email $emails */ public function removeemail(email $emails) { $emails->setuser(); $this->emails->removeelement($emails); } /** * set emails * * @param email[] $emails * @return user */ public function setemails($emails) { $this->emails = array(); foreach($emails $email) { $this->addemail($email); } return $this; } /** * emails * * @return email[] */ public function getemails() { return $this->emails; } }
entity\email
/** * email * * @orm\table() * @orm\entity */ class email { /** * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\column(name="email", type="string", length=255) * @assert\email(checkmx = false) */ private $email; /** * @var user * @orm\manytoone(targetentity="user", inversedby="emails") * @orm\joincolumn(name="user", referencedcolumnname="id", ondelete="cascade") * @assert\notnull() */ private $user; public function getid() { return $this->id; } /** * set email * * @param string $email * @return email */ public function setemail($email) { $this->email = $email; return $this; } /** * email * * @return string */ public function getemail() { return $this->email; } /** * set user * * @param user $user * @return email */ public function setuser(user $user = null) { $this->user = $user; return $this; } /** * user * * @return user */ public function getuser() { return $this->user; } }
usertype
/** * class usertype */ class usertype extends abstracttype { /** * @param formbuilderinterface $builder * @param array $options */ public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('emails', 'collection', array( 'type' => new emailtype(), 'allow_add' => true, 'allow_delete' => true )) ; } public function getname() { return 'user'; } }
emailtype
/** * class emailtype */ class emailtype extends abstracttype { /** * @param formbuilderinterface $builder * @param array $options */ public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('email', 'email') ; } /** * @param optionsresolverinterface $resolver */ public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'entity\user' )); } public function getname() { return 'email'; } }
the assert annotation included in entities.
the problem can save form without valid email address.
- 123@example.org <- thats saved <- thats correct
- 123 <- thats saved <- thats not correct / no valid email address!
an exception occurred while executing 'insert email (email, user) values (?, ?, ?)' params [null, null]
<- if use "by_reference" = true error , if "by_reference" false have no error...
i hope me :)
run command terminal:
php app/console doctrine:schema:update --force
Comments
Post a Comment