【发布时间】:2016-08-16 05:03:36
【问题描述】:
我有一个表单页面,其中一个字段名为type,它是enum('lost', 'found'),在这个表单中,我希望该字段是一个下拉列表,只有lost 和found 这两个选项.
建议的选项之一是在 view
<?= $form->field($model, 'type')->dropDownList(
$items,
['prompt'=>'']
这在 控制器
$items = ArrayHelper::map(Ads::find()->all(), 'id', 'type');
但正如您所知,它只是使用数据库中插入的数据,如果我单击下拉列表,它将加载数据库中所有丢失和找到的选项。
有没有办法告诉 yii 使用 db 结构和规则而不是数据?
我必须指出,在模型中我找不到任何指示枚举部分的规则,可以吗?为什么会这样?
我使用 Gii 来创建这些。
public function rules()
{
return [
[['type', 'explanation', 'image', 'cost', 'province_id', 'address'], 'required'],
[['type', 'explanation', 'image', 'address'], 'string'],
[['cost'], 'integer'],
[['province_id'], 'string', 'max' => 20],
[['province_id'], 'exist', 'skipOnError' => true, 'targetClass' => Province::className(), 'targetAttribute' => ['province_id' => 'name']],
];
}
【问题讨论】: