我正在处理一个问题,这个问题涉及到挂钩字段、设置默认值并将其隐藏起来。问题是它接受默认值,但只将值的第一个字符提交给数据库。
//Here is how I'm doing it
$form['field_sr_account'] = array( '#type' => 'hidden', '#value' => '45');我认为我构造数组的方式有问题,但我似乎不能得到它。我发现了一篇名为http://drupal.org/node/59660的帖子,其中有人只针对提交的第一个字符找到了解决方案
//Here is the format of the solution to the post - but it's not hidden
$form['field_sr_account'][0]['#default_value']['value'] = '45';如何才能将隐藏属性添加到其中?
发布于 2010-01-18 05:44:40
答案实际上是分别设置值和隐藏属性,然后在提交处理程序中使用以下格式再次设置值。
我不确定这是否都是必要的,我想我可能不需要在alter形式中指定它,但它是有效的,所以我将不管它……
$form['#field_sr_account'] = $club;
$form['field_sr_account'] = array( '#type' => 'hidden','#value' => $club);
}
}
/*in submit handler, restore the value in the proper format*/
$form_state['values']['field_sr_account'] = array('0' => array('value' => $form['#field_sr_account']));发布于 2010-01-11 10:14:55
你试过使用#value的#default_value实例吗?
此外,如果您试图向提交传递一些在表单中不会更改的数据,则应该使用http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html#value。
发布于 2012-05-20 06:52:54
来自http://drupal.org/node/257431#comment-2057358的有趣的解决方案
CCK隐藏字段
/**
* Implementation of hook_form_alter().
*/
function YourModuleName_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']) && isset($form['#node'])) {
### Make a CCK field becoming a hidden type field.
// ### Use this check to match node edit form for a particular content type.
if ($form_id === 'YourContentTypeName_node_form') {
$form['#after_build'] = array('_test_set_cck_field_to_hidden');
}
}
}
function _test_set_cck_field_to_hidden($form, &$form_state) {
$form['field_NameToBeHidden'][0]['value']['#type'] = 'hidden';
$form['field_NameToBeHidden'][0]['#value']['value'] = 'testValue';
return $form;
}https://stackoverflow.com/questions/2037846
复制相似问题