很多php框架,将form的生成写在函数,这是无可厚非的,但是你无法接收在生成一个form的时候,循环调用用户函数所付出的性能成本,这时候,构建一个php和html混编的代码,远比循环执行函数的性能要高。
而且,多数时候,我们知道一个数据对象的结构,也已经定义了数据对象的每个字段的值类型,我们真的没有必要再去手动写什么form,或者还要用代码去判断,这个用select,那个用textarea什么的。 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;?>> $column) { // 跳過$column[0]為空的部分 if (!isset($column[0])) continue; // 定制一行的$column常用變量 $key = strtolower(trim($key)); $columnid = $prefix . '_' . $key; $columnname = $prefix . '[' . $key . ']'; $columnclass = $prefix . '-' . $key; $columntext = empty($column['title']) ? ucfirst($key) : $column['title']; $columnvalue = isset($data[$key]) ? (string)$data[$key] : (isset($column['default']) ? $column['default'] : null); if (isset($column['forcevalue'])) $columnvalue = (string)$column['forcevalue']; $isdisabled = isset($column['disabled']) ? $column['disabled'] : false; $isreadonly = isset($column['readonly']) ? $column['readonly'] : false; if (is_object($column[0]) && $column[0] instanceof closure) { $edittype = 'closure'; } else { $edittype = strtolower($column[0]); }?> can't reuse dbo_form, please set the dbo_form options in $column[1].
widget('base/dbo_form', $column[1]); }?>
widget($column[1], $column[2]); ?>
if (isset($column['max'])) echo ' maxlength=',$column['max'],''; if ($isdisabled) echo ' disabled=disabled'; if ($isreadonly) echo ' readonly=readonly'; ?>/> if (isset($column['max'])) echo ' maxlength='.$column['max'].''; if ($isdisabled) echo ' disabled=disabled'; if ($isreadonly) echo ' readonly=readonly'; ?>> if (isset($column['max'])) echo ' maxlength=',$column['max'],''; if ($isdisabled) echo ' disabled=disabled'; if ($isreadonly) echo ' readonly=readonly'; ?>/> if (isset($column['max'])) echo ' maxlength='.$column['max'].''; if ($isdisabled) echo ' disabled=disabled'; if ($isreadonly) echo ' readonly=readonly'; ?>/> if ($isdisabled) echo ' disabled=disabled'; if ($isreadonly) echo ' readonly=readonly'; ?>> map, 1 -> list if (is_numeric($optionstype)) $optionstype = $optionstype > 0 ? 'list' : 'map';?> if ($isdisabled) echo ' disabled=disabled'; ?>> value=> $option) { ?> value=> value=> map, 1 -> list if (is_numeric($mapstype)) $mapstype = $mapstype > 0 ? 'list' : 'map';?> $map) { $index++;?>
0) { if (is_numeric($columnvalue)) { $dtstr = date($format, $columnvalue); $dtval = $columnvalue; } else { $dtstr = $columnvalue; $dtval = mst_string::date2num($columnvalue); } } else { $dtstr = 0; $dtval = 0; } if (is_numeric($dtval) && $dtval > 0) $dtstr = date($format, $dtval);?> type=text id=_str class=f-text f-dt value= /> type=hidden name= id= value= /> script('tiny_mce/tiny_mce.js'); ?> widget($column[1], $column[2]); break;}$linenum++;?>
$btntext) { $index++;?> validcode($prefix, 'input'); ?>
复制代码
widget('base/dbo_form', array( 'data' => $this->list,));
复制代码
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); } }}
复制代码
