Yii 模型中attributeLabels()函数的作用

下面这个函数的作用:有两点
1、后面的是前面的注释,在rules验证的时候,如果报错,会把此处的后面的内容显示出来
2、hint作用,即表单中用户插入数据时,提示用户该字段该填什么内容

Yii 模型中attributeLabels()函数的作用_第1张图片

 public function attributeLabels()
    {
        return [
            'cateid' => '分类名称',
            'title'  => '商品名称',
            'descr'  => '商品描述',
            'price'  => '商品价格',
            'ishot'  => '是否热卖',
            'issale' => '是否促销',
            'saleprice' => '促销价格',
            'num'    => '库存',
            'cover'  => '图片封面',
            'pics'   => '商品图片',
            'ison'   => '是否上架',
            'istui'   => '是否推荐',
        ];
    }


View中调用

Yii 模型中attributeLabels()函数的作用_第2张图片

                                echo $form->field($model, 'cateid')->dropDownList($opts, ['id' => 'cates']);
                                echo $form->field($model, 'title')->textInput(['class' => 'span9']);
                                echo $form->field($model, 'descr')->textarea(['id' => "wysi", 'class' => "span9 wysihtml5", 'style' => 'margin-left:120px']);
                                echo $form->field($model, 'price')->textInput(['class' => 'span9']);
                                echo $form->field($model, 'ishot')->radioList([0 => '不热卖', 1 => '热卖'], ['class' => 'span8']);
                                echo $form->field($model, 'issale')->radioList(['不促销', '促销'], ['class' => 'span8']);
                                echo $form->field($model, 'saleprice')->textInput(['class' => 'span9']);
                                echo $form->field($model, 'num')->textInput(['class' => 'span9']);
                                echo $form->field($model, 'ison')->radioList(['下架', '上架'], ['class' => 'span8']);
                                echo $form->field($model, 'istui')->radioList(['不推荐', '推荐'], ['class' => 'span8']);
                                echo $form->field($model, 'cover')->fileInput(['class' => 'span9']);


页面显示效果

Yii 模型中attributeLabels()函数的作用_第3张图片

你可能感兴趣的:(Yii)