如何删除$form->选择中的空值选项



当我需要使用select表单时,我看到第一个值是空的。。但是我不需要这个空值选项。。如何做到这一点。。感谢

 <?php
    $options = array('M' => 'Male', 'F' => 'Female');
    echo $this->Form->select('gender', $options)
    ?>

将输出:

<select name="data[User][gender]" id="UserGender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>

在Cake 2.x中,您可以像这样添加'empty'=>false(已测试并有效):

<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, array('empty'=>false));
?>

在CakePHP 1.3.x中(根据本书的这一页),您可能需要添加一个额外的null,如下所示:

<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, null, array('empty'=>false));
?>

最新更新