Donnerstag, März 08, 2012

isUnique Validator for lithium (li3)

If you want to check your model against uniqueness. this snippet might help you:
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;
});

Keine Kommentare: