I've written this as a module, I apologize for that but I don't know how to write .patch files :(
/**
* This module adds the "valid username" validation method
*
* @author Joakim Hedlund <joakim.hedlund@klarna.com>
*/
/**
* Implementation of hook_webform_validation_validators().
*
* @return array Validators, in the validator key => options array form.
*/
function webform_validation_uservalidator_webform_validation_validators(){
$validationMethods = array();
$validationMethods['username'] = array(
'name' => "Usernames",
'component_types' => array(
'textfield',
),
'description' => t('Verifies that the user-entered value is a valid username.'),
);
return $validationMethods;
}
/**
* Implementation of hook_webform_validation_validate().
*/
function webform_validation_uservalidator_webform_validation_validate($validator_name, $items, $components, $rule) {
if ($items) {
switch ($validator_name) {
case 'username':
foreach ($items as $key => $val) {
//load user
//if uid 0 or status 0 throw error
if ($val) {
if (!db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s' AND status = 1", $val))) {
// User doesn't exist
$errors[$key] = t('%item does not contain an active username.', array('%item' => $components[$key]['name']));
}
}
}
return $errors;
break;
}
}
}
Comments
Comment #1
justplaincorey commentedThis code looks like just what I need with some modification. But, forgive me, how/where do I use it with respect to my webform?
Comment #2
Sleavely commentedYou'll need to create a new module (in my case its called webform_validation_uservalidator (hence the confusing function names)), but first you need the Webform Validation module since this code uses a hook from that one.
Once you've installed the module you should be able to add this validation to any textfield just like you'd add any other validation method through the Webform Validation module. (see: http://drupal.org/node/908596)
Comment #3
justplaincorey commentedSo ... essentially, save the above code with .module extension and install it? Sounds good to me. :) Thanks!
Comment #4
justplaincorey commentedBut now, I end up with WSOD after pushing the module to the server.
Comment #5
Sleavely commentedYou'll probably have to write a .info file first, but yes. That's what I did. Here's my .info file:
As for the WSOD: The only thing I can think of is to ask whether you removed the closing part of the php tag ( ?> ). If you haven't, please do as it may cause trouble sometimes.
Comment #6
Seph commentedI believe I can adapt this to something I'm doing as well. I have created a simple webform that would screen out people that were not actually referred by current website members. I am using the registration codes module to generate referral codes that will be assigned to members. When a member refers someone to our website, that person will enter the member name and referral code into the webform. Upon submitting the form, the referral code would be validated against the member name. If they didn't match, the visitor would be directed to an invalid member/referral code page. If validated, they would be redirected to the traditional thank you for your submission page. Is this possible?
I am new to Drupal and writing code etc., so any help would be greatly appreciated.
Comment #7
neopoet commentedI believe this is simpler as a modification to the webform validation module (rather than as a standalone module). A patch for 7.x is attached.
Comment #8
svendecabooterThanks for the patch.
I'll check it soon...
Comment #9
svendecabooterThis has been committed and will be in the next release
Comment #10
neopoet commentedTerrific! Glad I could contribute.