0

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 Jan 3 '14 at 8:45
0

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:

drupal_get_form('user_register_form');

And print it like this:

print(drupal_render(drupal_get_form('user_register_form')));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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