0

Let me start by saying I have seen quite a few similar threads, and I have tried everything I've found so far, with none of them working.

My brother wanted a contact form to be in the footer of his site, so it gets rendered on every page. I didn't think this would be an issue, so I created a custom form and rendered it in a block. It looks great, but when I went to add in the validation and submit handlers, none of them work.

Here is what I currently have.

function opal_contact_form() {
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => 'name',
    '#required' => true,
    '#default_value' => '',
    '#attributes' => array('placeholder' => 'Name'),
  );
  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => 'Email',
    '#required' => true,
    '#default_value' => '',
    '#attributes' => array('placeholder' => 'Email'),
  );
  $form['phone'] = array(
    '#type' => 'textfield',
    '#title' => 'Phone',
    '#attributes' => array('placeholder' => 'Phone'),
    '#default_value' => '',
  );
  $form['company'] = array(
    '#type' => 'textfield',
    '#title' => 'Company',
    '#attributes' => array('placeholder' => 'Company'),
  );
  $form['topic'] = array(
    '#type' => 'textfield',
    '#title' => 'Topic',
    '#attributes' => array('placeholder' => 'Topic'),
    '#default_value' => '',
  );
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => 'Message',
    '#required' => true,
    '#default_value' => '',
    '#rows' => 3,
    '#attributes' => array('placeholder' => 'Your message...'),
  );
   $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  //$form['#submit'][] = 'opal_contact_form_submit';
  //$form['actions']['submit']["#submit"][] = "opal_contact_form_submit";
  kpr($form);
  return $form;
}

function opal_contact_form_validate($form, &$form_state) {
  kpr($form);
  dd('this is from the validate');
  exit();
}

function opal_contact_form_submit($form, &$form_state) {
  kpr($form);
  dd('this is from the submit');
  exit();
}

I don't think this is relevant, but here is the block rendering part.

function opal_blocks_block_view($delta) {
  $block = array();

  switch ($delta) {
    case 'contact_form':
      $block['subject'] = t('');
      $block['content'] = build_contact_form();
      break;
  }

  return $block;
 }

 function build_contact_form() {
   $form = drupal_get_form('opal_contact_form');
   $html = drupal_render($form);

   return $html;
 }

The parts commented out in the form definition are suggestions I found elsewhere and tried. I've never had to do anything special for a standard page form, except add _validate and _submit to the name of the form in order to get a validation and submit handler to work. Perhaps things work differently when rendered in a block?

Any suggestions would be appreciated.

EDIT

I found the problem in a theme function that I didn't include in the initial post cause i didn't think it would have anything to do with it, but I obvious was wrong. I am still not sure why this is causing the problem, but when I comment it out, it works fine. I just wanted my form to be displayed in 2 columns.

function opal_blocks_theme() {
  return array (
    'opal_contact_form' => array(
      'render element' => 'form'
    ),
  );
}

function theme_opal_contact_form($variables) {
  $form = $variables['form'];
  $output = '';
  $output .= '<h2>We would love to hear from you:</h2>';
  $output .= '<div class="form-left col-sm-6">';
  $output .= drupal_render($form['name']);
  $output .= drupal_render($form['email']);
  $output .= drupal_render($form['phone']);
  $output .= drupal_render($form['company']);
  $output .= '</div>';

  $output .= '<div class="form-right col-sm-6">';
  $output .= drupal_render($form['topic']);
  $output .= drupal_render($form['message']);
  $output .= drupal_render($form['submit']);
  $output .= '</div>';

  return $output;
}

2 Answers 2

1

You forget to render technical form fields: form_id, form_build_id, etc. As fix I suggest you to add the following to your theme function:

$output .= '<div class="element-hidden">' . drupal_render_children($form) . '</div>';
0
0

Those are the only 3 differences I can see between (working) block forms I have written in the past and your code.

1) What are the exit(); lines there for? Maybe try removing those?

2) I usually also define $form and $form_state in the original form function.

3) I do usually add the render array straight to the block content, maybe prerendering that as html causes some interference? (bit unlikely but worth a try)

function opal_blocks_block_view($delta) {
  $block = array();

  switch ($delta) {
    case 'contact_form':
      $block['subject'] = t('');
      $block['content']['form'] = drupal_get_form('opal_contact_form'); // Block content can handle renderable arrays, saves you a couple of lines of coding
      break;
  }

  return $block;
 }
3
  • The exit() lines are just to terminate as a debugging method. I did define $form and $form_state in the original form, i took them out based on another posts suggestion. Neither worked. I was getting strict warning messages when i had it all one one line as you have in point 3). I was not specifying the array key "form" though. I have never seen that. Commented May 26, 2015 at 23:52
  • Yeah, it was supposed to be wrapped as a string, fixed that in the post. I once ran into a very odd bug with form #prefix/suffix items being wrapped around the block. Since then I always nest accordingly. Still your code looks perfectly fine so I'm afraid I'm out of ideas. If I have a minute I'll try adding it to a custom module of my own. Commented May 27, 2015 at 11:00
  • Thanks for looking. I edited the original post with some new code that was causing the problem, but i'm not sure why. Commented May 27, 2015 at 13:05

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.