Skip to content

Instantly share code, notes, and snippets.

@agborkowski
Last active December 17, 2015 13:49
Show Gist options
  • Save agborkowski/5619553 to your computer and use it in GitHub Desktop.
Save agborkowski/5619553 to your computer and use it in GitHub Desktop.
lithium #li3 model validator configs #user-exist #user-active #is-unique
public $validates = [
'email_id' => [
[
'notEmpty',
'email',
'required' => true,
'message' => 'Enter a valid email',
'last' => true
],
// [
// 'UserActive',
// 'message' => 'Please activate your Account by click link in activation email',
// 'last' => true
// ],
[
'UserExists',
'message' => 'You are already a subscribed user. Please login to continue.'
]
],
'password' => [
[
'notEmpty',
'required' => true,
'message' => 'Please supply a password.'
]
]
];
Validator::add('UserExists', function($value) {
return (boolean) Users::findByEmailId($value) === true ? false : true;
});
Validator::add('UserActive', function($value) {
$value = Users::findByEmailId($value);
if ($value) {
$value = $value->status;
} else {
$value = true;
}
return (boolean) $value == true ? true : false;
});
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