I'm using Drupal 7 and I have some PHP pages that depends on my Drupal site.
In the PHP page, I tried to add a user programmatically in order the check the exception:

try {
  $new_user = array(
    'name' => 'user'
  user_save('', $new_user);
} catch (Exception $e) {
  echo $e->getMessage();

I didn't give all the required field, like email and some custom fields that I set in Home » Administration » Configuration » People » Account settings, but Drupal still added this user.

I need the following:

  1. Get all user field(s) and check if they are required or not
  2. Get error on exception when I'm trying to add user without required fields

How should I do that?

  • Drupal added the user because you told it to. The validation is tied to form submission, an unfortunate design choice. There's a function to submit forms in code though, which is possibly a place to start for you.
    – Letharion
    Commented Jan 3, 2014 at 8:45

1 Answer 1


I think you shouldn't create user this way, as it's bypassing all the controls made by drupal.

You can retrieve registration form this way:


And print it like this:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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