Created
October 30, 2011 16:47
-
-
Save jhartikainen/1326104 to your computer and use it in GitHub Desktop.
Example Doctrine 2 uniqueness validator for Zend_Form or such
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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