我的_form模型中有一个下拉列表,我想添加一个空值(我想要默认值).我有以下内容:在_form中:
<?php echo $form->labelEx($model,'country_id'); ?>
<?php echo $form->dropDownList($model,'country_id',Country::items(),array('empty' => '--Select a country--')); ?>
<?php echo $form->error($model,'country_id'); ?>
Run Code Online (Sandbox Code Playgroud)
在模范国家:
public static function items()
{
return CHtml::listData(Country::model()->findAllBySql(
'SELECT * from country'),
'id', 'name');
}
Run Code Online (Sandbox Code Playgroud)
即使我的空选项位于下拉列表的第一行,列表中的第一个国家/地区也显示为默认选项.
我试过了:
<?php echo $form->dropDownList($model,'country_id',
Country::items(),array('empty'=>'--Select a country--',
'options'=>
array(
'3'=>array('selected'=>'selected')
)
));
?>
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我可以选择默认选项,但不能将其设置为空值,只是来自model:items的国家/地区.
任何的想法?
我有一个Yii表单,它调用另一个模型的渲染部分(团队has_many team_members).我想通过ajax调用部分视图来添加team/_form中的成员.所有工作(调用,显示,保存)除了ajax验证(服务器和客户端).如果我提交表单,会员的模型不会验证,即使在客户端,它也不会验证必填字段.
任何线索?
//_形成
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'team-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>true
),
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
Run Code Online (Sandbox Code Playgroud)
//控制器
public function actionMember($index)
{
$model = new TeamMember();
$this->renderPartial('_member',array(
'model'=> $model, 'index'=> $index
)
,false,true
);
}
public function actionCreate()
{
$model=new Team;
$members = array();
if(isset($_POST['Team']))
{
$model->attributes=$_POST['Team'];
if(!empty($_POST['TeamMember'])){
foreach($_POST['TeamMember'] as $team_member)
{
$mem = new TeamMember();
$mem->setAttribute($team_member);
if($mem->validate(array('name'))) $members[]=$mem;
}
}
$this->redirect(array('team/create','id'=>$model->id,'#'=>'submit-message'));
}
$members[]=new TeamMember;
$this->performAjaxMemberValidation($members);
$this->render('create',array(
'model'=>$model,'members'=>$members
));
}
Run Code Online (Sandbox Code Playgroud)
//_会员
<div class="row-member<?php echo $index; ?>"> …Run Code Online (Sandbox Code Playgroud)