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 6750939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:52:03+00:00 2026-05-26T12:52:03+00:00

I’m using Doctrine 2 in a Zend Framework application and require functionality similar to

  • 0

I’m using Doctrine 2 in a Zend Framework application and require functionality similar to Zend_Validate_Db_RecordExists and Zend_Validate_Db_NoRecordExists.

For example, when a user enters a new item, I need to validate that a duplicate entry doesn’t already exist. This is easy to accomplish with Zend_Db by adding the Db_NoRecordExists validator on my forms.

I tried implementing the custom-validator solution proposed here, but I can’t figure out how they are communicating with Doctrine to retrieve entities (I suspect this approach may no longer work post-Doctrine 1.x).

The FAQ section of the Doctrine manual suggests calling contains() from the client code, but this only covers collections, and if possible I’d like to handle all of my form validation consistently from within my form models.

Can anyone suggest a way to use these Zend validators with Doctrine 2 DBAL configured as the database connection/resource?

  • 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-05-26T12:52:04+00:00Added an answer on May 26, 2026 at 12:52 pm

    It’s quite straightforward, really.

    I have a few Zend_Validate-type validators that talk to Doctrine ORM, so I have an abstract class that they descend from.

    Here’s the abstract class:

    <?php
    namespace TimDev\Validate\Doctrine;
    
    abstract class AbstractValidator extends \Zend_Validate_Abstract{
      /**
       * @var Doctrine\ORM\EntityManager
       */
      private $_em;
    
    
      public function __construct(\Doctrine\ORM\EntityManager $em){
        $this->_em = $em;
      }
    
      public function em(){
        return $this->_em;
      }
    }
    

    Here’s my NoEntityExists validator:

    <?php
    namespace TimDev\Validate\Doctrine;
    
    class NoEntityExists extends AbstractValidator{
    
      private $_ec = null;
      private $_property = null;
      private $_exclude = null;
    
      const ERROR_ENTITY_EXISTS = 1;
    
      protected $_messageTemplates = array(
        self::ERROR_ENTITY_EXISTS => 'Another record already contains %value%'  
      );
    
      public function __construct($opts){
        $this->_ec = $opts['class'];
        $this->_property = $opts['property'];
        $this->_exclude = $opts['exclude'];
        parent::__construct($opts['entityManager']);
    
      }
    
      public function getQuery(){
        $qb = $this->em()->createQueryBuilder();
        $qb->select('o')
                ->from($this->_ec,'o')
                ->where('o.' . $this->_property .'=:value');
    
        if ($this->_exclude !== null){ 
          if (is_array($this->_exclude)){
    
            foreach($this->_exclude as $k=>$ex){                    
              $qb->andWhere('o.' . $ex['property'] .' != :value'.$k);
              $qb->setParameter('value'.$k,$ex['value'] ? $ex['value'] : '');
            }
          } 
        }
        $query = $qb->getQuery();
        return $query;
      }
      public function isValid($value){
        $valid = true;
    
        $this->_setValue($value);
    
        $query = $this->getQuery();
        $query->setParameter("value", $value);
    
        $result = $query->execute();
    
        if (count($result)){ 
          $valid = false;
          $this->_error(self::ERROR_ENTITY_EXISTS);
        }
        return $valid;
    
      }
    }
    

    Used in the context of a Zend_Form (which has an em() method like the abstract class above):

    /**
       * Overrides superclass method to add just-in-time validation for NoEntityExists-type validators that
       * rely on knowing the id of the entity in question.
       * @param type $data
       * @return type 
       */
      public function isValid($data) {
        $unameUnique = new NoEntityExists(
                        array('entityManager' => $this->em(),
                            'class' => 'PMS\Entity\User',
                            'property' => 'username',
                            'exclude' => array(
                                array('property' => 'id', 'value' => $this->getValue('id'))
                            )
                        )
        );
        $unameUnique->setMessage('Another user already has username "%value%"', NoEntityExists::ERROR_ENTITY_EXISTS);
    
        $this->getElement('username')->addValidator($unameUnique);
    
        return parent::isValid($data);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but

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.