yii form rule小计

来源:互联网 发布:js注册协议 编辑:程序博客网 时间:2024/05/20 16:10

我喜欢这种写法

<?php
$form = $this->beginWidget('CActiveForm', array(
    'action'=> $this->createurl('train_add'),
    'id' => 'train_add-form',
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
    'enableClientValidation' => true,
    'clientOptions' => array(
    'validateOnSubmit' => true,
    ),
        ));
echo $form->errorSummary($model);
?>

//中间是form内容区
<?php $this->endWidget(); ?>


label标签
<?php echo $form->label(model'','名称')?>
<?php echo $form->label($model,'start_time')?>




select 列表  dropDownList
$form->dropDownList(model,名称,option值,属性-可以设置默认值)
<?php
echo $form->dropDownList($model,'stats',$lv_stats,array(
'options'=>array(
$stats=>array(
'selected'=>'selected'
))));
?>


yii中checkBoxList的用法是
<?php echo $form->checkBoxList($model,'fid',array(
'1'=>'中国',
'2'=>'日本',
'3'=>'美国',
'4'=>'德国',
),array('separator'=>'<br />','template'=>'{input} {label}')); ?>
 
我们把数据库中的

Controller中的actionCreate()方法中写入

$model->fid = array(1,2);//默认选中1和2选线

$this->render('opfunction',array(
'model'=>$model,
'B'=>$showB,
));

单个checkbox

<?=CHtml::checkbox('',false,array('value'=>$key,'title'=>$title))?>默认为不选中,如果默认选中checked,则将false改为true



text 文本框 textField  <?php echo $form-textField(model'','名称',属性-可设置默认值)?>


textarea
<?php echo $form->textarea($model,'名称',array('class'=>'textarea_text','style'=>'width:97%;'))?>




单选按钮组
<?php echo $form->radioButtonList($model,'songpi','数组', array('separator'=>'', 'template'=>'<span class="radio">{input} {label}</span> '));?>


日历插件 带时分秒的

Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
$this->widget('CJuiDateTimePicker', array(
'model' => $model, //Model object
'attribute' => 'peixun_time', //attribute name
'mode' => 'datetime', //use "time","date" or "datetime" (default)

 'options' => array(
'showAnim' => 'fold',
'dateFormat' => 'yy-mm-dd',
'changeYear' => true,
'changeMonth' => true,

),
'htmlOptions' => array(
'readonly' => 'readonly',
'style'    => 'width:95%;',
'value'    => '',
),
));


普通日历 这个是我用到比较多的

<?php
                    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                        'model' => $model,
'name' => 'start_time',
                        'attribute' => 'start_time',
                        // additional javascript options for the date picker plugin
                        'options' => array(
                            'showAnim' => 'fold',
                            // 'defaultData'=>date('Y-m-d',time()),
                            //'minDate'=>date('Y-m-d',time()),
                            'dateFormat' => 'yy-mm-dd',
                            'changeYear' => true,
                            'changeMonth' => true,
                        // 'yearRange'=>'-5,+5',
                        ),
                        'htmlOptions' => array(
                            'class' => 'put-text',
'style' => 'width:100px;',
                            'readonly' => 'readonly',
                            'value'=>'',
                        ),
'language' => 'zh',
                    ));
                    ?>


判断修改还是添加

<?php echo CHtml::submitButton($model->isNewRecord ? '增加' : '保存'); ?>



yii rule


return array(
   //必填
            array('description,goods_name', 'required'),
           //数字
            array('spec_qty, if_show, closed, recommended', 'numerical', 'integerOnly' => true),
   //长度限制
            array('com_id,user_id, type, cate_id', 'length', 'min'=>3,  'max' => 10),
            //存储字段
            array('description,brand,brand_id,goods_sn', 'safe'),
        );

原创粉丝点击