2010-12-15 34 views
0

领域我有形式如何填充在Drupal的形式API

function contactus(){ 
    return drupal_get_form('myform_contact'); 
    } 

function myform_contact($form_state){ 
    $form['field'] = array(
    '#type' => 'fieldset', 
    '#title' => 'Contact Us Form', 
    '#collapsible' => true, 
    '#collapsed'=> false, 
    '#description' => t('Enter your feedback'), 
    ); 

    $form['field']['email'] = array(
    '#type'=>'textfield', 
    '#title'=> 'E-mail Address', 
    '#required' => '0', 
    '#maxlength' => 127, 
    '#required' => 0, 
    ); 

    $form['field']['comment'] = array(
    '#type'=> 'textarea', 
    '#rows'=> 5, 
    '#title' => 'Comment/Question? ', 
    '#required' =>1,  
    ); 

    $form['field']['captcha'] = array(
    '#type' => 'captcha', 
    '#captcha_type' => 'image_captcha/Image', 

    ); 

    $form['field']['submit'] = array(
    '#type' => 'submit', 
    '#value' => 'Submit', 
    '#validate' => array('form_validate'), 
    '#weight' => 1 
    ); 
    $form['field']['edit'] = array(
    '#type'=> 'button', 
    '#value' => 'Edit', 
    '#validate'=> array('form_edit'), 
    '#weight' => 10, 
    ); 

    return $form; 
} 

function myform_contact_submit($form, &$form_state){ 

    $txt = $form_state['values']['comment']; 
    $email = $form_state['values']['email']; 
    $txt = wordwrap($txt,70); 
    mail("$email", "Comment/Questions ?", $txt); 
    $result = db_query("insert into contactus values('$email','$txt')"); 
    $node = new stdClass(); 
    $node-> title = $form_state['values']['email']; 
    $node-> body = $form_state['values']['comment']; 
    $node->type = 'contactus'; 
    node_save($node); 
    drupal_set_message(t('Form Successfully submitted with nid of '. $node->nid)); 
    drupal_set_message(t('Values inserted into Database')); 
} 

function form_edit($form,&$form_state){ 
    $nid = $form_state['values']['nid']; 
    $node = node_load(49); 
    drupal_set_message('edit mode with title '. $node->title. ' and comment as '. $node->body); 
    $form_state['values']['email'] = $node->title; 
    $form_state['values']['comment'] = $node->body;  
} 

在编辑我想填充值的表单字段的Drupal的API。但是,它不是填充。有任何想法吗??

回答

1

据我所知,FAPI没有form_edit类型的钩子。通常有一个表单定义函数,验证和提交函数。如果您尚未注意文档here

在我看来,您需要为要填充的表单元素添加'#default_value'属性。您还需要将加载节点的任何代码移动到myform_contact函数中。

+0

你能给我一个工作的例子,说明如何使用drupal_execute($ form_id,$ form_state)来实现表单填充吗? – user544079 2010-12-17 04:59:01