Skip to content

Instantly share code, notes, and snippets.

@michaeltwofish
Forked from heptat/validator.php
Created May 16, 2011 05:43
Show Gist options
  • Save michaeltwofish/973980 to your computer and use it in GitHub Desktop.
Save michaeltwofish/973980 to your computer and use it in GitHub Desktop.
validator
<?php
Validator::add('uniqueFieldForClient', function($value, $format = null, $options = array()) {
$class = $options['class'];
$field = $options['field'];
$conditions = array($field => $value);
if (isset($options['values']['id'])) {
$conditions[] = 'id != ' . $options['values']['id'];
}
// TODO should I throw an exception if client_id is missing? otherwise it's
// up to the models that use this validator to also validate for requiring
// client_id
$conditions['client_id'] = $options['values']['client_id'];
echo '<pre>';
print_r($conditions);
echo '</pre>';
$record = $class::find('first', array('conditions' => $conditions));
return !(bool) $record;
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment