Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
Created October 30, 2011 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhartikainen/1326104 to your computer and use it in GitHub Desktop.
Save jhartikainen/1326104 to your computer and use it in GitHub Desktop.
Example Doctrine 2 uniqueness validator for Zend_Form or such
<?php
use Doctrine\ORM\EntityRepository;
class Wantlet_Validate_EmailAvailable extends Zend_Validate_Abstract {
const NOT_AVAILABLE = 'notAvailable';
protected $_messageTemplates = array(
self::NOT_AVAILABLE => "Email address '%value%' is already in use"
);
private $repo;
public function __construct(EntityRepository $repo) {
$this->repo = $repo;
}
/**
* Returns true if and only if $value meets the validation requirements
*
* If $value fails validation, then this method returns false, and
* getMessages() will return an array of messages that explain why the
* validation failed.
*
* @param mixed $value
* @return boolean
* @throws Zend_Validate_Exception If validation of $value is impossible
*/
public function isValid($value) {
$this->_setValue($value);
if($this->repo->findOneByEmail($value)) {
$this->_error(self::NOT_AVAILABLE, $value);
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment