表格布局中的 Drupal 形式
我正在尝试为表单呈现表格布局(html 表),以便第一个单元格包含一个复选框。
$form = array();
$res = query('SELECT * FROM {mytable}');
$rows = array();
while($row = db_fetch_array($res)){
$record = array();
$checkbox = array(
'#type' => 'checkbox',
'#attributes' => array(
'value' => $row['id'],
'name' => 'myrecord[]',
),
);
$record[] = drupal_render($checkbox);
$record[] = $row['other_field_1'];
$record[] = $row['other_field_2']
$rows[] = $record;
}
$form['records'] = array(
'#value' => theme('table', array(), $rows);
);
return $form;
但所有呈现的复选框都有输出
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
名称和值属性未应用于复选框的问题是什么?
I am trying to render a tabular layout (html table) for a form such that first cell contains a checkbox.
$form = array();
$res = query('SELECT * FROM {mytable}');
$rows = array();
while($row = db_fetch_array($res)){
$record = array();
$checkbox = array(
'#type' => 'checkbox',
'#attributes' => array(
'value' => $row['id'],
'name' => 'myrecord[]',
),
);
$record[] = drupal_render($checkbox);
$record[] = $row['other_field_1'];
$record[] = $row['other_field_2']
$rows[] = $record;
}
$form['records'] = array(
'#value' => theme('table', array(), $rows);
);
return $form;
But all the rendered checkbox has output
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
<input type="checkbox" class="form-checkbox" name="" value="1" id="">
What is the issue that name and value attributes are not getting applied on checkboxes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用表单 API 时,无法使用属性设置名称和值。该名称与表单项的键相关,因此如果您有
第一个复选框的名称将为
pony
,而第二个复选框的名称将为yes
。我相信复选框的值始终为 1,但我不能 100% 确定。要正确制作此表单,您应该做的就是仅在表单定义中创建表单。执行类似的操作:
执行类似的操作,您可以在表单的主题函数中创建表。在主题函数中渲染表单元素,将为它们提供正确的名称。
The name and value is not something you can set using attributes, when using the form API. The name is related to the key of the form item, so if you had
The first checkbox will have the name of
pony
while the second will have the name ofyes
. I believe that the value for checkboxes always is 1, but I'm not 100% sure.What you should do, to make this form properly, is to only create the form, in the form definition. Doing something like this:
Doing something like this, you can create the table in a theme function for the form. Rendering the form elements in the theme function, will give them the correct name.