这是一个十分有用的组件,可用于生成一个数据的编辑form,他是MST Library 3.1一个十分重要的组件,可以实现dbo form和dbo form的循环嵌套,而且控制在一个form中,同时支持dbo form中再次嵌套自定义的widget组件。
  很多PHP框架,将form的生成写在函数,这是无可厚非的,但是你无法接收在生成一个form的时候,循环调用用户函数所付出的性能成本,这时候,构建一个php和html混编的代码,远比循环执行函数的性能要高。
  而且,多数时候,我们知道一个数据对象的结构,也已经定义了数据对象的每个字段的值类型,我们真的没有必要再去手动写什么form,或者还要用代码去判断,这个用select,那个用textarea什么的。				
    		
				
            	            		
            	            	
            
- 
// 要操作的對象的數據
 
- // $target必須為基於MST_DBO的實例
 
- if (!isset($data) || !is_object($data) || !$data instanceof MST_DBO) {
 
- 	echo '$data not a MST_DBO instance!';
 
- }
 
- else {
 
- 	// 獲取關聯的模塊
 
- 	$model = get_class($data);
 
- 
 - 	// 定義$columns
 
- 	// 如果未定義,默認根據MST_DBO的接口來取
 
- 	if (!isset($columns))
 
- 		$columns = $data->getFormColumns($data);
 
- 	if (empty($columns) || !is_array($columns)) {
 
- 		echo 'undefine form columns!';
 
- 	}
 
- 	else {
 
- 	
 
- 		// 生成該模塊的前綴
 
- 		if (!isset($prefix))
 
- 			$prefix = strtolower($model);
 
- 		else
 
- 			$prefix = MST_String::tableize($prefix);
 
- 
 - 		if (!isset($id))
 
- 			$id = $prefix . '_form';
 
- 
 - 		if (!isset($class))
 
- 			$class = $prefix . '-form';
 
- 
 - 		$errors = $data->getErrors();
 
- 		
 
- 		// 初始化Form配置
 
- 		// 定制提交的action
 
- 		if (!isset($action))
 
- 			$action = $this->params->uri;
 
- 
 - 		// method
 
- 		if (!isset($method))
 
- 			$method = 'post';
 
- 		else {
 
- 			$method = strtolower((string)$method);
 
- 			if ($method != 'get' && $method != 'post')
 
- 				$method = 'post';
 
- 		}
 
- 
 - 		// 是否需要上傳
 
- 		if (!isset($isUpload)) $isUpload = true;
 
- 
 - 		// 定制提交按鈕的文字
 
- 		if (!isset($submitText)) $submitText = 'Submit';
 
- 
 - 		// 定制label部分的寬度
 
- 		if (!isset($headWidth)) $headWidth = 130;
 
- 		$headWidth = is_numeric($headWidth) && $headWidth > 0 ? $headWidth : 120;
 
- 
 - 		if (!isset($continueForm)) $continueForm = false;
 
- 
 - 
 - 		// 重載
 
- 		if (!isset($lineStart)) $lineStart = 1;
 
- ?>
 
- 
	}
 
- }
 
- ?>
 
  
复制代码
         		            	
            	            		
            	            	
            
- 
$this->widget('base/dbo_form', array(
 
- 	'data' => $this->list,
 
- ));
 
- 
   
复制代码
         		            	
            	            		
            	            	
            
- class Testimonial extends MST_DBO {
 
- 
 - 	protected static
 
- 		$columns = array(
 
- 			'firstname' => array('text','title' => 'First Name', 'require' => 1, 'min' => 1, 'max' => 32),
 
- 			'lastname' => array('text','title' => 'Last Name', 'require' => 1, 'min' => 1, 'max' => 32),
 
- 			'avator' => array('title' => 'Avator', 'max' => 256),
 
- 			'age_group' => array('title' => 'Age Group', 'require' => 1),
 
- 			'secret' => array('textarea','title' => 'Secret', 'require' => 1, 'min' => 10, 'max' => 600),
 
- 		);
 
- 
 - 	public function  getFormColumns() {
 
- 		if (GB_PERSSIONS == Region::ROOT) {
 
- 			$columns['region_id'] = array(
 
- 				'select',
 
- 				'title' => ' region ',
 
- 				'optionsType' => 'list',
 
- 				'options' =>  Region::find('all', array('select' => 'id, name')),
 
- 			);
 
- 		}
 
- 		else {
 
- 			$columns['region_id'] = array(
 
- 				'hidden',
 
- 				'default' => GB_PERSSIONS,
 
- 			);
 
- 		}
 
- 		$columns = array_merge($columns,self::$columns);
 
- 		$columns['age_group'] = array('widget', 'base/age_group', array(
 
- 			'prefix' => 'testimonial',
 
- 		), 'title' => 'Age Group');
 
- 		$columns['avator'] = array('widget', 'base/testmonial_upload', array(
 
- 			'prefix' => 'testimonial',
 
- 		), 'title' => 'Avator');
 
- 		
 
- 		return $columns;
 
- 	}
 
- 
 - 	public function beforeCreate(& $data) {
 
- 		$data['created_at'] = time();
 
- 	}
 
- 
 - 	public function getAge() {
 
- 		$ageGroup = array(
 
-                          0 => '--',
 
- 			 1 => 'Under 18',
 
- 			 2 => '19 ? 25',
 
- 			 3 => '26 ? 35',
 
- 			 4 => '36 ? 45',
 
- 			 5 => '46 ? 55',
 
- 			 6 => '56 or above',
 
- 		);
 
- 		return isset($ageGroup[$this['age_group']]) ? $ageGroup[$this['age_group']] : $ageGroup[0];
 
- 	}
 
- 
 - 	public function getAvator() {
 
- 		return empty($this['avator']) ? httpUri('images/avator.png') : httpUri($this['avator']);
 
- 	}
 
- 	
 
- 	// 这是对MST_DBO的find的方法的重载
 
- 	static public function find($args = array(), $params = null, $isArray = false) {
 
- 		if (defined('GB_PERSSIONS') && GB_PERSSIONS == Region::ROOT) {
 
- 			self::initFind($args, $params, $isArray);
 
- 			return parent::find($args, $params, $isArray);
 
- 		}
 
- 		else {
 
- 			self::initFind($args, $params, $isArray);
 
- 			if (isset($args['where'])) {
 
- 				$args['where'][0] .= ' AND region_id = ?';
 
- 				$args['where'][] = GB_PERSSIONS;
 
- 			}
 
- 			else {
 
- 				$args['where'] = array('region_id = ?', GB_PERSSIONS);
 
- 			}
 
- 			return parent::find($args, $params, $isArray);
 
- 		}
 
- 	}
 
- }
 
  
复制代码
         		            	
            	            		
            	            	
					
				
				
            	 
 |