I need to populate the form fields programmatically in drupal. I understand that there are 2 approaches:
开发者_运维技巧using drupal_execute($form_item,value,$form_state)
using form_set_value($form_id,$form_state)
Any working examples would be helpful
I tried the following code which gives the drupal white screen of death
function form_validate($form, &$form_state){
$form_id ='myform';
$form_state['values'] = array(
'name' => 'Test',
);
drupal_execute($form_id, $form_state); // this statement leads to white screen of death
}
I assume you are trying to fill out a field in the form during validation, after the user submits it? I assume this because you are using a validate function.
If so, there is no need for the drupal_execute() function, as the form will be submitted after it passes validation no matter what. I think what you want might be more like this:
function form_validate($form, &$form_state) {
$form_state['values']['name'] = 'test';
}
You should use:
<?php
$form_id = 'mymodule_form_id';
$rendered_form = drupal_get_form($form_id);
print $rendered_form;
?>
See:
http://api.drupal.org/api/drupal/includes--form.inc/function/drupal_get_form/6
精彩评论