所以我一直试图让 select2Row 永远为我工作,但我似乎无法掌握它。我要做的是为用户提供列出学校/大学名称的标签,同时在我保存表单时存储所述标签的值。我注意到我不能同时使用数据和标签,否则我的下拉菜单不会填充,所以这不是一个选项。标签似乎只需要文本,而不是文本和匹配值。有任何想法吗?
<div class="row">
<?php
echo $form->select2Row($model, 'school_id', array(
'asDropDownList'=>false,
'options'=>array(
'tags'=>School::model()->schoolNames,
//'maximumSelectionSize'=>1,
'width'=>'297px',
'tokenSeparators'=>array(','),
),
));
?>
<?php echo $form->error($model,'school_id'); ?>
</div>
这是 schoolNames 的功能
public function getSchoolNames()
{
$schools = array();
$result = $this->findAllBySQL("SELECT DISTINCT name FROM School");
foreach($result as $school){
$schools[] = $school->name;
}
return $schools;
}
我已经尝试使用它,但标签不会填充
public function getSchools()
{
$query = "SELECT id,name FROM School";
$results = $this->findAllBySQL($query);
return CHtml::listData($results, 'id', 'name');
}
因此,目前,select2Row 正在使用 getSchoolNames() 生成一个标签列表,并且一切正常,除非您使用这些标签提交表单,否则您将得到这个
Please fix the following input errors:
School must be an integer.
抱歉给您带来了麻烦,我对此还是有点陌生。我敢肯定,答案可能就在我面前。无论哪种方式都感谢=)