1、首先要说的就是form的使用
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'license-form',//ajax验证的form id
'htmlOptions' => array(‘class’=>'','enctype'=>'multipart/form-data'),//自定义html元素
'enableClientValidation'=>false,//是否开启客户端验证
'enableAjaxValidation'=>false,//是否开启ajax验证
'clientOptions'=>array(
'validateOnSubmit'=>true, //提交时是否验证
),
)); ?>
结束标记:<?php $this->endWidget(); ?>
当然还可以使用CHtml的方式:
<?php echo CHtml::beginForm($this->createUrl('/admin‘),'get')?>
<?php echo CHtml::endForm(); ?>
2、一些常用的基本组件使用示例
输入框textfields:echo $form->textField($model,'title',array('class'=>'ipt-long'));
密码框passwordfields: echo $form->passwordField($model,'password',array('class'=>'loginIn'));
标签label: echo $form->labelEx($model,'email');
区域textarea: echo $form->textArea($model,'area',array('class'=>''));
链接link: echo CHtml::link(CHtml::encode('删除'), array('/user'),array('class' => 'confirm'));
按钮button:CHtml::button('关闭',array(οnclick="location.href='<?php echo $this->createUrl('/trade/close');?>'">))
提交按钮submitbutton: echo CHtml::submitButton('下一步',array('class'=>'btn'));
错误信息,红色字体不换行输出:
错误提示error: echo $form->error($model,'password',array('style'=>'color:red;display:inline'));
3、单选按钮radiobuttonlist
<?php echo $form->radioButtonList($model,'cate', array('1'=>'项目','2'=>'技术'),array('separator'=>' '));?>
4、下拉菜单dropdownlist
<?php echo $form->DropDownList($model,'cate',array('1'=>'食品','2'=>'机械') ?>
5、复选框checkbox
<?php echo $form->checkBoxList($model,'cooperation', array('1'=>'合作开发','2'=>'直接购买','3'=>'其他:'),array('separator'=>' '));?>
6、验证码
<?php $this->widget('CCaptcha',array('showRefreshButton'=>true,'buttonLabel'=>'看不清?换一张','clickableImage'=>true,'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer'))); ?>
7、日历组件
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'name' => 'Post[endtime]',
'language'=>'zh_cn',
'options'=>array(
//'showAnim'=>'fold',
//'showOn'=>'both',
//'buttonImage'=>Yii::app()->request->baseUrl.'/images/calendar.gif',
'maxDate'=>'new Date()',
//'buttonImageOnly'=>true,
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:25px',
'maxlength'=>8,
),
));
?>
8、编辑器
//配置引用ueeditor
'controllerMap'=>array(
'ueditor'=>array(
'class'=>'ext.ueditor.UeditorController',
'config'=>array('initialFrameWidth'=>500),
'thumbnail'=>false,//是否开启缩略图
'watermark'=>'',//水印图片的地址,使用相对路径
'locate'=>9,//水印位置,1-9,默认为9在右下角
'savePath'=>'xxx',
'allowFiles'=>array(
'image' => array('.gif', '.png', '.jpg', '.jpeg'),
'file' => array( '.doc' , '.docx' , '.zip' , '.pdf' , '.txt'),
),
),
),
<?php echo $form->textArea($model,'companyinfo',array('class'=>'editor')); ?>
<?php $this->widget('ext.ueditor.UeditorWidget',
array(
'id'=>'info',//初始化容器ID
'name'=>'info',//指定ueditor实例的名称
)
); ?>
<?php echo $form->error($model,'companyinfo'); ?>
显示编辑器提交的内容:
<?php
$this->beginWidget('CMarkdown', array('purifyOutput'=>true));
echo $model->info;
$this->endWidget();
?>
9、yii ajax省市联动的实现
<?php echo CHtml::DropDownList('provinceid',$model->provinceid,Area::getProvinceList(),
array(
'ajax' => array(
'type' => 'POST',
'url' => $this->createUrl('/area/updatecity'),
'data' => array('provinceid' => 'js:this.value'),
'update' => '#cityid'
))); ?>
<?php echo CHtml::DropDownList('cityid',$model->cityid,Area::getCityList($model->provinceid)); ?>
<?php echo $form->error($model,'cityid'); ?>
updatecity中的代码如下,需要三级联动可自行参考添加
public function actionUpdateCity()
{
if (isset($_POST['provinceid'])){
$data = Area::getCity(intval($_POST['provinceid']));
foreach($data as $value=>$name){
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name['name']),true);
}
}else {
return array();
}
}
10、上传组件
<?php echo $form->fileField($model,'pic');?> OR <?php echo CHtml::activeFileField($model,'pic'); ?>
上传代码如下:
//$image = CUploadedFile::getInstanceByName('image');
$pic = CUploadedFile::getInstance($model, 'pic');
if($pic !== null){
$pic->saveAs('/image/'.date('Ymd').'/<span><span>'.time().</span><span class="string">'_'</span><span>.rand(0,9999).</span><span class="string">'.'</span><span>.</span><span class="vars">$file</span><span>->extensionName</span></span>);
}
model中的上传配置:
array('pic', 'file', 'allowEmpty'=>false,
'types'=>'jpg, jpeg, gif, png',
'maxSize'=>1024 * 1024 * 2, // 2M
'tooLarge'=>'上传文件超过 2M,无法上传。',
),
显示图片:
<?php echo CHtml::image(Yii::app()->baseUrl.$model->pic,$model->title,array('width'=>'200px','height'=>'150px'));?>
<?php echo CHtml::link('查看大图',Yii::app()->baseUrl.$model->pic,array('target'=>'_blank'));?>
11、yii的下载
$model =xxx::model()->findByPk($id);
$type =$model->filetype;
$content = file_get_contents($model->downpath);
Yii::app()->request->sendFile($model->title.'.'.$model->type,$content);