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框
原理参见 源代码 CForm类的renderElement方法片段, 如果没有设置则不会进行elements中每一个element的渲染;而button的显示条件不一样,所以会显示出来。
需要在model类中加入rules方法,如:
就可以显示出来了
或者这样:
都是为了让元素具有可见性。