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;
	}
    }
}
CommentFileSizeAuthor
#7 webform_validation-1053548.patch1.18 KBneopoet

Comments

justplaincorey’s picture

This code looks like just what I need with some modification. But, forgive me, how/where do I use it with respect to my webform?

Sleavely’s picture

You'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)

justplaincorey’s picture

So ... essentially, save the above code with .module extension and install it? Sounds good to me. :) Thanks!

justplaincorey’s picture

But now, I end up with WSOD after pushing the module to the server.

Sleavely’s picture

You'll probably have to write a .info file first, but yes. That's what I did. Here's my .info file:

name = Webform validation username extension
description = "Validates a field against the user database"
package = Webform
dependencies[] = webform_validation
core=6.x

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.

Seph’s picture

I 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.

neopoet’s picture

Version: 6.x-1.3 » 7.x-1.0-rc1
Assigned: Unassigned » neopoet
Status: Patch (to be ported) » Needs review
StatusFileSize
new1.18 KB

I 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.

svendecabooter’s picture

Assigned: neopoet » svendecabooter

Thanks for the patch.
I'll check it soon...

svendecabooter’s picture

Status: Needs review » Fixed

This has been committed and will be in the next release

neopoet’s picture

Terrific! Glad I could contribute.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.