Last active
December 17, 2015 13:49
-
-
Save agborkowski/5619553 to your computer and use it in GitHub Desktop.
lithium #li3 model validator configs #user-exist #user-active #is-unique
This file contains 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
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