2013-11-18 19:34:47 3496次浏览 3条回答 0 悬赏 10 金钱

model:User.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author Administrator
 */
class User extends CActiveRecord {

    //静态类
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    //表名
    public function tableName() {
        return '{{user}}';
    }

    //cform配置文件
    public function getFMConfig() {
        return array(
            'elements' => array(
                'name' => array('type' => 'text', 'maxlength' => 80),
                'password' => array('type' => 'password', 'maxlength' => 80),
            ),
            'buttons' => array(
                'button' => array('type' => 'submit', 'label' => '提交'),
            ),
        );
    }

}
--------------------------------
controller UserController.php
<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserController
 *
 * @author Administrator
 */
class UserController extends Controller{
    public function actionRegister(){
        $user = new User();
        $form = new CForm($user->getFMConfig(), $user);

        $this->render('register',array('form'=>$form));
    }

    
}

--------------------------------

view :register.php

<?php echo $form ?>
----------------------------------------------------------------------

现象:只显示提交按钮。不显示,input框

如果在elemtns属性中设置label 属性,也能显示,就是不显示正常input框



最佳答案

  • icultivator 发布于 2013-11-19 12:38 举报

    原理参见 源代码 CForm类的renderElement方法片段, 如果没有设置则不会进行elements中每一个element的渲染;而button的显示条件不一样,所以会显示出来。

  • 回答于 2013-11-19 11:38 举报

    需要在model类中加入rules方法,如:

    public function rules(){
    	return array(
    		// username and password are required
    		array('username, password', 'required'),
    	);
    }
    

    就可以显示出来了

  • 回答于 2013-11-19 12:31 举报

    或者这样:

    'elements'=>array(
    	'username'=>array(
    		'type'=>'text',
    		'maxlength'=>32,
    		'visible'=>true,
    	),
    	'password'=>array(
    		'type'=>'password',
    		'maxlength'=>32,
    		'visible'=>true,
    	)
    ),
    

    都是为了让元素具有可见性。

您需要登录后才可以回答。登录 | 立即注册
sbfnxk201
见习主管

sbfnxk201

注册时间:2013-09-20
最后登录:2014-02-26
在线时长:30小时0分
  • 粉丝0
  • 金钱0
  • 威望0
  • 积分300

热门问题