好吧,这可能是我缺少的东西,但不知怎的,我的字段集没有验证。这就是我现在所拥有的。
我的实体:
USER:
<?php
namespace User\Model\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation as ZFA;
/**
* @ORM\Entity
* @ORM\Table(name="Users")
*
* @ZFA\Name("user")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @ORM\OrderBy({"name" = "ASC"})
* @ZFA\Exclude()
* @var int
*/
private $id;
/**
* @ORM\Column(type="string")
* @ZFA\Filter({"name":"StringTrim"})
* @ZFA\Required(true)
* @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
* @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
* @ZFA\Attributes({"type":"text"})
* @ZFA\Options({"label":"Username:"})
* @var string
*/
private $username;
/**
* @ORM\Column(type="string")
* @ZFA\Filter({"name":"StringTrim"})
* @ZFA\Required(true)
* @ZFA\Validator({"name":"StringLength", "options":{"min":8}})
* @ZFA\Attributes({"type":"text"})
* @ZFA\Options({"label":"Password:"})
* @var string
*/
private $password;
/**
* @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
* @ORM\JoinTable(name="User_users_groups")
**/
private $groups;
/**
* @ORM\OneToMany(targetEntity="Profile",cascade={"persist", "remove"}, mappedBy="user")
*/
private $profile;
public function __construct() {
$this->groups = new ArrayCollection();
$this->profile = new ArrayCollection();
}
public function addProfile(Collection $profiles)
{
foreach ($profiles as $profile) {
$profile->setUser($this);
$this->profile->add($profile);
}
}
public function removeProfile(Collection $profiles)
{
foreach ($profiles as $profile) {
$profile->setUser(null);
$this->profile->removeElement($profile);
}
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getId()
{
return $this->id;
}
public function setUsername($username)
{
$this->username = $username;
return $this;
}
public function getUsername()
{
return $this->username;
}
public function setPassword($password)
{
$this->password = $password;
return $this;
}
public function getPassword()
{
return $this->password;
}
public function setProfile(Profile $profile) {
$this->profile = $profile;
$profile->setUser($this);
}
public function getProfile()
{
return $this->profile;
}
}
PROFILE:
<?php
namespace User\Model\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation as ZFA;
/**
* @ORM\Entity
* @ORM\Table(name="Profiles")
* @ZFA\Name("profile")
* @ZFA\Type("fieldset")
*/
class Profile
{
/**
*
* @var int $id
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @ZFA\Exclude()
*/
private $id;
/**
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="profile")
* @ZFA\Exclude()
*/
private $user;
/**
*
* @ORM\Column(type="string")
* @ZFA\Required(true)
* @ZFA\Filter({"name":"StringTrim"})
* @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
* @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
* @ZFA\Attributes({"type":"text"})
* @ZFA\Options({"label":"Firstname:"})
* @var string
*/
private $givenName;
/**
*
* @ORM\Column(type="string")
* @ZFA\Required(true)
* @ZFA\Filter({"name":"StringTrim"})
* @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
* @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
* @ZFA\Attributes({"type":"text"})
* @ZFA\Options({"label":"Surname:"})
* @var string
*/
private $surName;
/**
* @ORM\Column(type="string")
* @ZFA\Required(true)
* @ZFA\Filter({"name":"StringTrim"})
* @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
* @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
* @ZFA\Attributes({"type":"text"})
* @ZFA\Options({"label":"Initials:"})
*/
private $initial;
/**
* @ORM\Column(type="string")
* @ZFA\Required(true)
* @ZFA\Filter({"name":"StringTrim"})
* @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
* @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
* @ZFA\Attributes({"type":"text"})
* @ZFA\Options({"label":"Display name:"})
*/
private $displayName;
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getId()
{
return $this->id;
}
public function setUser($user)
{
$this->user = $user;
return $this;
}
public function getUser()
{
return $this->user;
}
public function setGivenName($givenName)
{
$this->givenName = $givenName;
return $this;
}
public function getGivenName()
{
return $this->givenName;
}
public function setSurName($surName)
{
$this->surName = $surName;
return $this;
}
public function getSurName()
{
return $this->surName;
}
public function setInitial($initial)
{
$this->initial = $initial;
return $this;
}
public function getInitial()
{
return $this->initial;
}
public function setDisplayName($displayName)
{
$this->displayName = $displayName;
return $this;
}
public function getDisplayName()
{
return $this->displayName;
}
}
表单创建:
在service.config.php(测试的临时位置)
'factories' => array(
'User\Form\AddUserForm' => function($sm) {
$objectManager = $sm->get('doctrine.entitymanager.orm_default');
$builder = new \Zend\Form\Annotation\AnnotationBuilder();
$profileFieldset = $builder->createForm('User\Model\Entity\Profile');
$profileFieldset
->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($objectManager, 'User\Model\Entity\Profile'))
->setObject(new \User\Model\Entity\Profile())
->setUseAsBaseFieldset(true);
$form = $builder->createForm('User\Model\Entity\User');
$form->add($profileFieldset);
$form->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($objectManager, 'User\Model\Entity\User'));
$form->setObject(new \User\Model\Entity\User());
$form->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'sitecheck'
));
$form->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save'
)
));
return $form;
}
)
ACTION:
public function createAction()
{
$user = new \User\Model\Entity\User();
$form = $this->getServiceLocator()->get('User\Form\AddUserForm');
$form->bind($user);
var_dump($this->getRequest()->getPost());
if ($this->request->isPost()) {
$form->setData($this->getRequest()->getPost());
if ($form->isValid()) {
//save stuff isn't relevant here
}
}
return new ViewModel(array(
'form' => $form,
));
}
当我提交表单时,它将验证“用户”部分,但忽略了字段集(required = true除外)。
我尝试了我现有的知识所允许的所有内容。所以如果有人能够指出我的方向非常好。
答案 0 :(得分:1)
您应该尝试在用户实体
中的个人资料条目上使用ComposedObject
注释
答案 1 :(得分:0)
Wauw解决方案非常简单..
感谢Gabriel Baker
在用户实体中我必须添加
/**
* ...
* @ZFA\ComposedObject("User\Model\Entity\Profile")
* ...
*/
private $profile;
将表单创建修改为
$objectManager = $sm->get('doctrine.entitymanager.orm_default');
$builder = new \Zend\Form\Annotation\AnnotationBuilder();
$form = $builder->createForm('User\Model\Entity\User');
$form->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($objectManager, 'User\Model\Entity\User'));
$form->setObject(new \User\Model\Entity\User());
$form->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'sitecheck'
));
$form->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save'
)
));
return $form;