So i got a ZF2 application, got a Form and a InputFilter in the InputFilter i have:
$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'
),
),
),
)
);
works great, however when i edit a existing object and save it the NoObjectExists validator says a matching object is found so it doesn't validate. Is there a solution to this problem? Or should i just remove the validator on the edit form and catch the exception when a duplicate is inserted?
UPDATE: How to use DoctrineModule\Validator\NoObjectExists in edit forms - Zend Framework 2 & Doctrine 2
is the same issue but the answer is to just remove the validator on editing, this off-course is not a solution. As you would still have to catch the exception thrown for when inserting a duplicate. I could do that no problem but what im asking for is a solution to make it work WITH NoObjectExists (otherwise whats the use of this validator if i have to catch the exception for duplicates anyway)
UPDATE, added other relevant code (my form and entity have more fields than this but i removed them to keep it readable on here)
FORM:
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',
),
)
);
}
}
FILTER:
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'
),
),
),
)
);
}
}
CONTROLLER ACTION:
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
);
}
ENTITY:
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;
}
}
thanks in advance,
Yrm