Last active
August 29, 2015 14:27
-
-
Save dopsmain/4db1ae56bd34652da973 to your computer and use it in GitHub Desktop.
FormIt custom validator. Checks if the username is already taken by another user
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
<?php | |
/** | |
* checkUsername | |
* FormIt custom validator. Check if no other user has the same username | |
* | |
* Usage: | |
* &validate=`yourfield:checkUsername` | |
* To set an custom error text; use like default ones; | |
* &vTextCheckUsername=`Your custom error text!` | |
* | |
* NOTE: Add to FormIt with &customValidators=`checkUsername` | |
* | |
* @author Jeroen Kenters <jeroen@kenters.com> | |
*/ | |
//get logged in username (or empty when no logged in user) | |
$currentUser = $modx->getLoginUserName(); | |
//if current user is same as posted value, proceed | |
if($currentUser == $value) { | |
return true; | |
} | |
//not the same, so check if another user already has this username | |
$count = $modx->getCount('modUser', array('username' => $value)); | |
if($count > 0) { | |
$validator->addError($key, $validator->_getErrorMessage($key,'vTextCheckUsername','Username already taken!')); | |
return false; | |
} | |
return true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment