Search code examples
drupaldrupal-7user-registration

Drupal 7: cancel user registration if a wrong field value has been submitted


I have a Drupal 7 with 1 field added to registration form - a List with possible values Robot / Male / Female: enter image description here

# select * from field_data_field_gender;
 entity_type | bundle | deleted | entity_id | revision_id | language | delta | field_gender_value
-------------+--------+---------+-----------+-------------+----------+-------+--------------------
 user        | user   |       0 |         6 |           6 | und      |     0 | Male
 user        | user   |       0 |         5 |           5 | und      |     0 | Male
 user        | user   |       0 |         7 |           7 | und      |     0 | Female
 user        | user   |       0 |         1 |           1 | und      |     0 | Male

The first value Robot is a default value - to prevent SPAM robots from registering at my site. Is there a way to cancel user registration, when a new user submits the registration web form with the default Robot value? (i.e. only Male and Female values are allowed).

I've looked at the core Trigger module, but don't see anything suitable there.

I've looked at the hook_user_presave and hook_user_insert API docs, but don't see a way there to cancel an ongoing user registration. (I was expecting to do that by returning a special value from those methods...)

Thank you! Alex


Solution

  • Use a hook_form_alter and add a new validate function where you'll check the value of the field you want to check.