I am trying to create a form which calls an external template file to format the form but ended up into a typical issue. This is what I was doing. I created a hook menu and called a theme, in the theme declared the template, and in template called the form elements and rendered along with html code. Everything is working fine except the submit button. If I render form directly, submit is working, or if I use drupal_get_form in menu page callback, it works (again template is not getting applied). I'm confused what am I doing wrong. I am a newbie so excuse me if this is a repetitive or stupid question.
Here is my code. My module file named agents.module
<?php
function agents_menu(){
$items=array();
$items['myform']=array(
'title'=>'Form',
'description'=>'Submit Form into database',
'page callback'=>'agents_form',
'access callback'=>TRUE,
'access arguments'=>array('access content'),
'type'=>MENU_CALLBACK,
);
return $items;
}
function agents_form(){
return theme('students');
}
function agents_theme(){
return array(
'students' => array(
'render element' => 'form',
'template' => 'customTemplate',
),
);
}
function students_form($form, &$form_state){
$form = array();
$form['efname'] = array(
'#type' => 'textfield',
'#title' => 'First Name',
'#size' => 28,
'#maxlength' => 10,
'#required' => TRUE,
);
$form['elname'] = array(
'#type' => 'textfield',
'#title' => 'Last Name',
'#size' => 28,
'#maxlength' => 10,
'#required' => TRUE,
);
$form['submit_btn'] = array(
'#type'=>'submit',
'#value'=>'Submit',
'#submit' => 'agents_myForm_submit'
);
return $form;
}
function students_form_validate($form, &$form_state){
dpm($form_state['ename']);
}
function students_form_submit(){
dpm('into submit');
}
and my customTemplate.tpl.php code looks like this
<?php
dpm(drupal_get_form('students_form'));
$form = drupal_get_form('students_form');
?>
<div class="left_div">
<?php print render($form['efname']); ?>
<?php print render($form['elname']); ?>
</div>
<div class="right_div">
<?php print render($form['submit_btn']); ?>
</div>
<?php
dpm(render($form['form_build_id']));
print render($form['form_build_id']);
print render($form['form_token']);
print render($form['form_id']);
?>
<?php print drupal_render_children($form);
I am a newbie to drupal, and I was not able to figure out what am I doing wrong. If I directly render the form without rendering submit individually, (i.e. print render($form) ) it works fine. It would be a great savior of the day if someone helps me cross this hurdle.