为什么CakePHP在表单中呈现下拉菜单而不是文本框?

时间:2009-08-17 10:34:14

标签: php cakephp textbox drop-down-menu

我正在尝试使用从数据库中检索到的vales创建一个文本框。但是,不是文本框,而是创建选择框(下拉框)。我不懂为什么。我只给出了文本类型,然后为什么要创建一个选择框?

echo $form->input($r['Attribute']['label'], 
                  array('label' => $r['Attribute']['label'].'  * ',
                        'class' => 'required',
                        'id'    => $r['Attribute']['id'],
                        'name'  => $r['Attribute']['label'],
                        'type'  => 'text',
                        'style' => 'width:' . $r['Attribute']['size'] . 'px'));

这是包含一些记录的Attributes表。

id  form_id  label  type    size  sequence_no  required
2   1        Name   text    200   1            true
3   1        Age    number  200   2            true 

$ form->输入的输出是

<div class="input select">
<label for="4">Name * </label>
<select id="4" class="required" style="width: 200px;" name="data[Name]"> </select>
</div>

而不是

<div class="input text">
<label for="4">Name * </label>
<input id="4" class="required" style="width: 200px;" name="data[Name]"> </input>
</div>

即使我明确地将输入类型称为“文本”,输入类型如何保存为“选择”?

3 个答案:

答案 0 :(得分:2)

因为Attribute.label的值是大写的,所以CakePHP认为它是对belongsTo关系的另一个模型的引用,因此它会尝试自动为您提供列表。

尝试更换:

$form->input($r['Attribute']['label'],

有类似的东西:

$form->input('Attribute.'.$r['Attribute']['id'].'.label',

应输出:

<div class="input text">
<label for="4">Name * </label>
<input id="4" class="required" style="width: 200px;" name="data[Attribute][2][label]" />
</div>

这将为您提供CakePHP将识别的结构中所需的所有信息。

编辑:哦,并更改具有'id' => $r['Attribute']['id'],的行,因为这只会生成无意义的HTML属性。像'id' => 'Attribute'.$r['Attribute']['id']这样的东西应该更有用。

答案 1 :(得分:1)

我唯一能想到的是,如果您有一个名为“name”的字段,并且您的视图中还有一个名为"$names"的变量,并且此变量是一个数组,通过命名惯例蛋糕可能认为这些属于一起。

通过FormHelper,实际上是这段代码:

$types = array('text', 'checkbox', 'radio', 'select');
if (!isset($options['options']) && in_array($options['type'], $types)) {
    // ... looks for corresponding variable in the view ...
    $options['type'] = 'select';

这似乎是Cake可以自行更改输入类型的特殊情况。

'options' => null参数中设置$form->input()看起来应该会有所帮助。

答案 2 :(得分:0)

你能做到吗? PR($ R [ '属性']); 在你的代码块之上并粘贴输出?通常你不需要那里的名字甚至类型。

查看以下链接以获取更多信息: http://book.cakephp.org/view/189/Automagic-Form-Elements http://api.cakephp.org/class/form-helper#method-FormHelperinput