I need a working example of how to repopulate form fields with the help 开发者_如何学Cof drupal_execute($form_id,$form_state). I can repopulate by altering the default_values as follows:
function sample_myform($form_state){
$form['field']['name'] = array(
'#type' => 'textfield',
'#title'=> 'Name: ',
'#maxlength'=> 127,
'#default_value'=> $form_state['values']['name'],
);
$form['field']['button1'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function sample_myform_form_alter(&$form,$form_state,$form_id){
if($form_id=='sample_myform'){
$id = db_result(db_query("select max(id) from test2"));
$name = db_result(db_query("select name from test2 where id ='$id'"));
$form['field']['name']['#default_value'] = "$name";
drupal_set_message(t('Name: '.$name." id: ".$id));
}
}
However, i want to use drupal_execute for repopulation. Any suggestions?
drupal_execute calls drupal_prepare_form which calls hook_form_alter, so you should be fine. On the other hand, you could pass your values in $form_state['values']
like this:
$form_state['values']['name'] =
db_result(db_query("select name from test2 where id ='$id'"));
drupal_execute('sample_myform', $form_state);
However, if #tree is true
for the element, the first line should read
$form_state['values']['field']['name'] =
db_result(db_query("select name from test2 where id ='$id'"));
精彩评论