Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to make optional the mail field from the contact form. I manage to remove the asterisk by implementing the hook_form_alter:

function contact_field_form_alter(&$form, &$form_state, $form_id) {
    $form['mail']['#required'] = FALSE;

However on form submission, I get the message "You must enter a valid e-mail address". I seems that, although the field is optional on the form, it still tries to validate it. Can somebody tell me how to make it really optional?

Thanks in advance

share|improve this question
up vote 5 down vote accepted

Are you talking about the site wide contact form or the personal contact forms that each user can have? I'll talk about the site wide form, but the following roughly applies to the personal one as well.

The email validation is hard wired in contact_site_form_validate, and the $mail is also needed in the submission handler, so there is no nice way around it.

Easy way: Set the default value for the mail field to an address that you own and then disable access to the field like this:

function contact_field_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'contact_site_form') {
    $form['mail']['#required'] = FALSE;
    $form['mail']['#default_value'] = 'me@my-site.com';
    $form['mail']['#access'] = FALSE;

This will completely hide the mail field.

Hardcore way: In your form alter you could overwrite the default validation and submission callbacks (using $form['#validate'] and $form['#submit']) and replace them with your custom ones. This does not come without the risk of side effects. If you have other modules that want to add custom stuff and logic for contact forms, then this will probably break them.

As an alternative, you could use the webform module. It gives you a lot more flexibility concerning the information that you want to request and having an optional email field in there is very easy to do.

Update: You can also try the following (untested):

function contact_field_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'contact_site_form') {
    $form['mail']['#required'] = FALSE;
    $form['mail']['#default_value'] = '';
    // This makes our custom validation come first.
    array_unshift($form['#validate'], 'my_custom_validation');

function my_custom_validation($form, &$form_state) {
  // This fills an empty mail value with a default one.
  if (empty($form_state['values']['mail']) {
    form_set_value($form['mail'], 'me@my-site.com', $form_state);
  // After this the default validation will run.

This will remove the asterisk, hide any preset value and in the validation handler a default value for the mail field is set if none has been submitted by the user. That way the hard wired validation shouldn't have a reason to complain.

share|improve this answer
Thanks for your answer. I was talking about the system wide contact form. The problem with using: $form['mail']['#default_value'] = 'me@my-site.com'; is that the user that submits the form sees the default mail in the field. I don't want to disable or hide the field but make it optional. Any idea about a workaround using the hook? – codependent Feb 5 '14 at 21:55
@WornOutSoles See the updated answer. Maybe that helps you. – berliner Feb 5 '14 at 22:10
It works!! Thanks so much – codependent Feb 5 '14 at 23:23

Your Answer


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.