Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm trying to prevent a user from being saved with Rules when he/she tries to use a username that contains a whitespace. I've already read How can I disallow spaces in usernames? and used the User restrictions module to achieve this, but I'd like to use Rules to do some other stuff (e.g. checking that the username doesn't have more than 20 characters).

So, what I'd like to do is showing an error message on the page and keep all the filled in information in the form without saving it. Is it possible to do this?

This is the Rule I'm currently using:

  • Event: Before saving a user account.
  • Conditions: NOT Execute custom PHP code. <?php preg_match('/\s/',$account->name) == 1; ?>
  • Actions: Show a message on the site.
share|improve this question
    
Is it really necessary to use rules in this case? If you are open for custom codes then I can help you out. –  Aneek Mukhopadhyay Aug 2 at 16:47
    
@AneekMukhopadhyay: I always prefer Rules above code, because I can work faster with them, but custom coding is fine too. :) –  Jeroen Aug 2 at 16:52

2 Answers 2

@Jeroen, Then I would suggest to do just one custom form alter to the user registration form and add a validation function that will validate each case for you.

/**
 * Implements hook_form_FORM_ID_alter().
 */
// Alter the registration form
function CUSTOM_MODULE_form_user_register_alter(&$form, &$form_state, $form_id) {
  // Add your own function to the array of validation callbacks
  $form['#validate'][] = 'CUSTOM_MODULE_user_validation';
}

/**
 * Custom function to validate the user creations.
 */
function CUSTOM_MODULE_user_validation(&$form, &$form_state) {
  $name = $form_state['values']['name'];
  if (!preg_match('/\s/',$name)) {
     form_set_error('name', t('Name should not contain white spaces.'));
  }
}

Its easier to use and doesn't invoke rules API every time. I also think this is much effective for performance.

share|improve this answer
    
I agree. It is a far more maintainable solution. –  J. Reynolds Aug 2 at 17:21
    
Thanks for your help. Is this code already supposed to work? I've used it in my custom module, but there's no error thrown when a new user is registrated with a space in it's name. I'd also like to prevent this from happening when an existing user edits his/her account. –  Jeroen Aug 3 at 13:54
    
Hello, this should work. Have you cleared the Drupal cache after adding this bit of code? –  Aneek Mukhopadhyay Aug 3 at 15:32

I don't think using Rules for form validation is the right way to go. It is too messy and complicated.

I will rather do a form alter on the registration form and add my own custom validation handler.

Or try using Field Validation module. I have never used it on user fields before, but I don't see why it won't work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.