How to use DoctrineModule \ Validator \ NoObjectExists in editable forms - Zend Framework 2 & Doctrine 2

What is the most efficient way to use the DoctrineModule\Validator\NoObjectExists in the Zend form, which is also used for editing? Because when I use the same form to save the edited values, this confirms that the form of existence of the object and the flag is invalid.

+4
source share
3 answers

A few weeks ago, I solved the same problem using a helper method in my custom filter. I am not sure if this is the right approach, but it works.

  • Write a custom input filter that extends Zend\InputFilter\InputFilter .
  • Add your common filters and validators to the init() filter method.
  • Write a helper method in the input filter that adds the doctrine validator to the current validator chain, as shown below.
  • Add an existing validator to the filter instance when creating a new object. When editing, use an instance of the universal filter.

So,

 <?php /** * Baz filter */ class BazFilter extends Zend\InputFilter\InputFilter { /** * This method will be triggered automatically when you retrive baz filter via inputfiltermanager. */ public function init() { // Define your input names, types, validators and filters as arrays $this->add(array( 'name' => 'code', 'required' => true, 'validators' => array(), 'filters' => array() )); $this->add( array(...) ); $this->add( array(...) ); // ... } /** * Appends doctrine noobjectexists validator to validator chain only when required. * * @access public * @param \Doctrine\ORM\EntityRepository $repository * @return \Zend\InputFilter\InputFilter */ public function appendExistenceValidator(\Doctrine\ORM\EntityRepository $repository) { $validatorSignature = array( 'name' => 'code', 'validators' => array( array( 'name' => 'DoctrineModule\Validator\NoObjectExists', 'options' => array( 'object_repository' => $repository, 'fields' => 'code', 'messages' => array( NoObjectExists::ERROR_OBJECT_FOUND => "This object with code already exists in database." ) ) ) ) ); $validator = $this->getFactory()->createInput( $validatorSignature ); $this->add($validator); return $this; } } 

Finally, add this input filter to the form when editing:

 // $form = your form instance // $filter = Bazfilter instance $form->setData($postData)->setInputFilter( $filter ); if( $form->isValid() === false ) { // ... } 

While creating:

 // $filter = bazfilter instance $repository = $entityManager->getRepository('Your\Entity\Name'); $filter->appendExistenceValidator( $repository ); // <-- Notice this line $form->setData($postData)->setInputFilter( $filter ); if( $form->isValid() === false ) { // ... } 
+6
source

I tried using DoctrineModule \ Validator \ NoObjectExists in the form and prevents updates that keep unique fields intact. As @Tadej mentioned, use UniqueObject. Here is an example form:

 class ExampleForm extends Form implements InputFilterProviderInterface { /** * @var EntityManager */ private $entityManager; /** * @var Repository */ private $repository; /** * ExampleForm constructor. * * @param EntityManager $entityManager * @param Repository $repository */ public function __construct(EntityManager $entityManager, Repository $repository) { $this->entityManager = $entityManager; $this->repository = $repository; $this->add( [ 'type' => Text::class, 'name' => 'name', 'options' => [ 'label' => _('Name *') ], 'attributes' => [ 'class' => 'form-control', ], ] ); } /** * @return array */ public function getInputFilterSpecification() { return [ 'name' => [ 'required' => true, 'filters' => [ ], 'validators' => [ [ 'name' => UniqueObject::class, 'options' => [ 'object_manager' => $this->entityManager, 'object_repository' => $this->repository, 'fields' => ['name'], 'use_context' => true, 'messages' => [ UniqueObject::ERROR_OBJECT_NOT_UNIQUE => "Name '%value%' is already in use", ] ] ] ] ], ]; } } 

Pay attention to the use of the "use_context" option - it should be used if the field is not a primary key. "messages" are optional.

0
source

Source: https://habr.com/ru/post/1234918/


All Articles