Skip to content

Instantly share code, notes, and snippets.

@m4rcsch
Created March 8, 2012 10:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save m4rcsch/2000376 to your computer and use it in GitHub Desktop.
Save m4rcsch/2000376 to your computer and use it in GitHub Desktop.
isUnique Validator for Lithium (li3) PHP Framework
Validator::add('isUnique', function($data, $params, $options) {
$model = $options['model'];
$field = $options['field'];
$key = $model::meta('key');
$entity = $model::first(array('conditions' => array($field => $data)));
$identifier = isset($options['values'][$key]) ? $options['values'][$key] : null;
if ($entity && $entity->data() && $identifier == (string) $entity->$key) {
return false;
}
return true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment