Validation in Zend Framework 2 with Doctrine 2

我与影子孤独终老i 提交于 2019-11-30 23:42:29

I had the same problem and solved it this way:

  1. Create a custom validator class, name it something like NoEntityExists (or whatever you want).
  2. Extend Zend\Validator\AbstractValidator
  3. Provide a getter and setter for Doctrine\ORM\EntityManager
  4. Provide some extra getters and setters for options (entityname, ...)
  5. Create an isValid($value) method that checks if a record exists and returns a boolean
  6. To use it, create a new instance of it, assign the EntityManager and use it just like any other validator.

To get an idea of how to implement the validator class, check the validators that already exist (preferably a simple one like Callback or GreaterThan).

Hope I could help you.

// Edit: Sorry, I'm late ;-)

So here is a quite advanced example of how you can implement such a validator.

Note that I added a translate() method in order to catch language strings with PoEdit (a translation helper tool that fetches such strings from the source codes and puts them into a list for you). If you're not using gettext(), you can problably skip that.

Also, this was one of my first classes with ZF2, I wouldn't put this into the Application module again. Maybe, create a new module that fits better, for instance MyDoctrineValidator or so.

This validator gives you a lot of flexibility as you have to set the query before using it. Of course, you can pre-define a query and set the entity, search column etc. in the options. Have fun!

<?php
namespace Application\Validator\Doctrine;

use Zend\Validator\AbstractValidator;
use Doctrine\ORM\EntityManager;

class NoEntityExists extends AbstractValidator
{
    const ENTITY_FOUND = 'entityFound';

    protected $messageTemplates = array();

    /**
     * @var EntityManager
     */
    protected $entityManager;

    /**
     * @param string
     */
    protected $query;

    /**
     * Determines if empty values (null, empty string) will <b>NOT</b> be included in the check.
     * Defaults to true
     * @var bool
     */
    protected $ignoreEmpty = true;

    /**
     * Dummy to catch messages with PoEdit...
     * @param string $msg
     * @return string
     */
    public function translate($msg)
    {
        return $msg;
    }

    /**
     * @return the $ignoreEmpty
     */
    public function getIgnoreEmpty()
    {
        return $this->ignoreEmpty;
    }

    /**
     * @param boolean $ignoreEmpty
     */
    public function setIgnoreEmpty($ignoreEmpty)
    {
        $this->ignoreEmpty = $ignoreEmpty;
        return $this;
    }

    /**
     *
     * @param unknown_type $entityManager
     * @param unknown_type $query
     */
    public function __construct($entityManager = null, $query = null, $options = null)
    {
        if(null !== $entityManager)
            $this->setEntityManager($entityManager);
        if(null !== $query)
            $this->setQuery($query);

        // Init messages
        $this->messageTemplates[self::ENTITY_FOUND] = $this->translate('There is already an entity with this value.');

        return parent::__construct($options);
    }

    /**
     *
     * @param EntityManager $entityManager
     * @return \Application\Validator\Doctrine\NoEntityExists
     */
    public function setEntityManager(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
        return $this;
    }

    /**
     * @return the $query
     */
    public function getQuery()
    {
        return $this->query;
    }

    /**
     * @param field_type $query
     */
    public function setQuery($query)
    {
        $this->query = $query;
        return $this;
    }

    /**
     * @return \Doctrine\ORM\EntityManager
     */
    public function getEntityManager()
    {
        return $this->entityManager;
    }

    /**
     * (non-PHPdoc)
     * @see \Zend\Validator\ValidatorInterface::isValid()
     * @throws Exception\RuntimeException() in case EntityManager or query is missing
     */
    public function isValid($value)
    {
        // Fetch entityManager
        $em = $this->getEntityManager();

        if(null === $em)
            throw new Exception\RuntimeException(__METHOD__ . ' There is no entityManager set.');

        // Fetch query
        $query = $this->getQuery();

        if(null === $query)
            throw new Exception\RuntimeException(__METHOD__ . ' There is no query set.');

        // Ignore empty values?
        if((null === $value || '' === $value) && $this->getIgnoreEmpty())
            return true;

        $queryObj = $em->createQuery($query)->setMaxResults(1);

        $entitiesFound = !! count($queryObj->execute(array(':value' => $value)));

        // Set Error message
        if($entitiesFound)
            $this->error(self::ENTITY_FOUND);

        // Valid if no records are found -> result count is 0
        return ! $entitiesFound;
    }
}
dawinterfeldt

if you use the DoctrineModule, there is already a validator for your case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!