所以我得到了一个 ZF2 应用程序,在我拥有的 InputFilter 中得到了一个 Form 和一个 InputFilter:
$this->add(
array(
'name' => 'email',
'required' => true,
'validators' => array(
array(
'name' => 'EmailAddress'
),
array(
'name' => 'DoctrineModule\Validator\NoObjectExists',
'options' => array(
'object_repository' => $sm->get('doctrine.entitymanager.orm_default')->getRepository('YrmUser\Entity\User'),
'fields' => 'email'
),
),
),
)
);
效果很好,但是当我编辑现有对象并保存它时,NoObjectExists 验证器说找到了匹配的对象,因此它不会验证。这个问题有解决方案吗?或者我应该删除编辑表单上的验证器并在插入重复项时捕获异常?
更新: 如何在编辑表单中使用 DoctrineModule\Validator\NoObjectExists - Zend Framework 2 & Doctrine 2
是同样的问题,但答案是在编辑时删除验证器,这不是解决方案。因为您仍然必须捕获插入副本时引发的异常。我可以做到这一点没问题,但我要求的是一个解决方案,使其与 NoObjectExists 一起工作(否则,如果我必须捕获重复项的异常,这个验证器的用途是什么)
更新,添加了其他相关代码(我的表单和实体的字段比这更多,但我删除了它们以使其在此处可读)
形式:
namespace YrmUser\Form;
use Zend\Form\Form;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use DoctrineORMModule\Stdlib\Hydrator\DoctrineEntity;
use YrmUser\Entity\User;
class UserForm extends Form
{
protected $objectManager;
/**
* __construct description
*
* @param String $name form name
*
* @return void
*/
public function __construct($name = null)
{
parent::__construct('new-user');
}
public function init()
{
$this->setHydrator(
new DoctrineHydrator($this->objectManager, 'YrmUser\Entity\User')
)->setObject(new User());
$this->setAttribute('method', 'post');
$this->add(
array(
'name' => 'email',
'attributes' => array(
'type' => 'email',
'placeholder' =>'Email',
),
'options' => array(
'label' => 'Email',
),
)
);
}
}
筛选:
class UserFilter extends InputFilter
{
/**
* [__construct description]
*
* @param ServiceLocator $sm servicelocator
*/
public function __construct($sm)
{
$this->add(
array(
'name' => 'email',
'required' => true,
'validators' => array(
array(
'name' => 'EmailAddress'
),
array(
'name' => 'DoctrineModule\Validator\NoObjectExists',
'options' => array(
'object_repository' => $sm->get('doctrine.entitymanager.orm_default')->getRepository('YrmUser\Entity\User'),
'fields' => 'email'
),
),
),
)
);
}
}
控制器动作:
public function editAction()
{
$id = (int) $this->params('id', null);
if (null === $id) {
return $this->redirect()->toRoute('manage-users');
}
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$formManager = $this->getServiceLocator()->get('FormElementManager');
$form = $formManager->get('UserForm');
$user = $em->find('YrmUser\Entity\User', $id);
$form->setInputFilter(new UserFilter($this->getServiceLocator()));
$form->bind($user);
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$em->persist($user);
$em->flush();
return $this->redirect()->toRoute('manage-users');
}
}
return array(
'form' => $form,
'id' => $id
);
}
实体:
class User
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string", unique=true, length=255)
*/
protected $email;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* @param int $id user id
*
* @return void
*/
public function setId($id)
{
$this->id = (int) $id;
}
/**
* Get email.
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set email.
*
* @param string $email user email adress
*
* @return void
*/
public function setEmail($email)
{
$this->email = $email;
}
}
提前致谢,
年限