Skip to content

Instantly share code, notes, and snippets.

@dopsmain
Last active August 29, 2015 14:27
Show Gist options
  • Save dopsmain/4db1ae56bd34652da973 to your computer and use it in GitHub Desktop.
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
<?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