1

For my site, I have to create a custom form in a custom template. It is not in a custom block, but in a custom tpl-file. I have searched the internet for a solution, but or the form is not shown or the fields are shown, but not in a < form > tag. This way, the submit-button does not work. When I render the fields in a self-written form-tag, the validate function and submit-function don't work.

Can someone help me?

TPL-File

$form = _odisee_forms($form, $dag, $datum, $uur, $campussen);
$form['infodag'] = form_process_checkboxes($form['infodag']);
$formulier = drupal_get_form('infodagen-block-form');
print '<form id="'.$formulier['#id'].'" accept-charset="UTF-8" method="'.$formulier['#method'].'" action="'.$formulier['#action'].'">'; 
print drupal_render_children($form); 
//print render($form);
print render($formulier['form_id']);
print render($formulier['form_build_id']);
print render($formulier['form_token']);
print '</form>';

forms.inc

function _odisee_forms($type, $dag, $datum, $uur, $campussen) {
  $form = array();
  foreach ($campussen as $key => $campus) {
  $infomoment[] = t($dag[$key])." ".$datum[$key]. " @ " .$campus. " om ".$uur[$key];
  }
  $form['#id'] = 'infodagen-block-form';
  $form['infodag'] = array(
    '#type' => 'checkboxes',
    '#options' => $infomoment,
    '#required' => TRUE,
  );
  $form['voornaam'] = array(
    '#type'  => 'textfield',
    '#title' => t('<img src="/sites/all/modules/custom/odisee_forms/images/icon.png" class="icon">'),
    '#size'  => 15,
    '#attributes' => array(
      'placeholder' => array('Voornaam'),
      'class' => array('breed')
    ),
    '#required' => TRUE,  
  ); 
  $form['naam'] = array(
    '#type'  => 'textfield',
    '#title' => t('<img src="/sites/all/modules/custom/odisee_forms/images/icon.png" class="icon">'),
    '#size'  => 15,
    '#attributes' => array(
      'placeholder' => array('Naam'),
      'class' => array('breed')
    ),
    '#required' => TRUE,  
  );
  $form['emailadres'] = array(
    '#type' => 'textfield', 
    '#title' => t('<img src="/sites/all/modules/custom/odisee_forms/images/arroba-symbol.png" class="icon">'),
    '#size' => 20, 
    '#maxlength' => 128, 
    '#required' => TRUE,
    '#attributes' => array(
      'placeholder' => array('E-mailadres'),
      'class' => array('breed')
    ),
  );
  $form['submit'] = array(
    '#type'  => 'submit',
    '#value' => t('Schrijf in!'),
    '#attributes' => array('class' => array('button')),
    '#submit' => array('_odisee_form_submit'),
    '#validate' => array('_odisee_form_validate'),
  );
  return $form;
}


function _odisee_forms_validate($form_id, &$form, &$form_state) {
  dpm($form_state, "state");
  dpm($form);
}

function _odisee_forms_submit($form, &$form_state) {
}
0

You need to render your form in the template. Simply calling drupal_get_form won't suffice. To render the form use following:

drupal_render(drupal_get_form('form_nam'))

You also need to ensure the following:

  • In your custom form #theme parameter is set to appropriate theme function.
    • Further the hook_theme implementation is correct.

Refer this link for a working example

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.