我正在处理一个问题,我正在挂钩一个字段,设置默认值,并隐藏它。问题是它采用默认值,但只将值的第一个字符提交给数据库。
//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';
如何将隐藏属性添加到此?
答案 0 :(得分:2)
您是否尝试过使用#default_value
代替#value
?
此外,如果您尝试将某些数据传递给不会在表单中更改的提交,则应使用http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html#value。
答案 1 :(得分:1)
答案实际上是分别设置值和隐藏属性,然后使用以下格式在提交处理程序中再次设置值。
我不确定是否一切都是必要的,我想我可能不需要以改变形式分配它,但它有效,所以我将不管它...
$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']));
答案 2 :(得分:1)
来自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;
}