Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:18:40+00:00 2026-06-10T20:18:40+00:00

I am right now getting myself more and more familiar with Zend Framework 2

  • 0

I am right now getting myself more and more familiar with Zend Framework 2 and in the meantime I was getting myself updated with the validation part in Zend Framework 2. I have seen few examples how to validate the data from the database using Zend Db adapter, for example the code from the Zend Framework 2 official website:

//Check that the username is not present in the database
$validator = new Zend\Validator\Db\NoRecordExists(
    array(
        'table' => 'users',
        'field' => 'username'
    )
);
if ($validator->isValid($username)) {
    // username appears to be valid
} else {
    // username is invalid; print the reason
    $messages = $validator->getMessages();
    foreach ($messages as $message) {
        echo "$message\n";
    }
}

Now my question is how can do the validation part?

For example, I need to validate a name before inserting into database to check that the same name does not exist in the database, I have updated Zend Framework 2 example Album module to use Doctrine 2 to communicate with the database and right now I want to add the validation part to my code.

Let us say that before adding the album name to the database I want to validate that the same album name does not exist in the database.

Any information regarding this would be really helpful!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:18:41+00:00Added an answer on June 10, 2026 at 8:18 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using git for version control and right now we are getting a
I need some help. Here's what I'm getting right now: img ref I need
Just getting into SQL stored queries right now... anyway, here's my database schema (simplified
Right now I have an upload field while uploads files to the server. The
Right now, I have: RewriteRule ^([^/\.]+)?$ index.php?id=$1 [L] to match any username at the
Right now I have a script that will get the last five files in
Right now I have 3 tables: User, Roles, and User_Roles for the many-to-many association.
Background I have a few scripts that run as part of my build process
I'm starting to find myself getting more and more in to using WCF for
So i'm getting into security problems right now, and i want to know what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.