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;
}