This is a very useful component that can be used to generate a data editing form. It is a very important component of MST Library 3.1. It can realize the loop nesting of dbo form and dbo form, and control it in one form. It also supports dbo The custom widget component is nested again in the form.
Many PHP frameworks write form generation in functions. This is understandable, but you cannot accept the performance cost of cyclically calling user functions when generating a form. At this time, build a mixed PHP and HTML Code, the performance is much higher than the function of loop execution.
Moreover, most of the time, we know the structure of a data object and have defined the value type of each field of the data object. We really don’t need to manually write any form, or use code to judge. This uses select , that uses textarea or something.
- //Data of the object to be operated
- // $target must be an instance based on MST_DBO
- if (!isset($data) || !is_object($data) || !$data instanceof MST_DBO) {
- echo '$data not a MST_DBO instance!';
- }
- else {
- // Get the associated module
- $model = get_class($data);
- // Define $columns
- // If not Definition, taken by default according to the MST_DBO interface
- if (!isset($columns))
- $columns = $data->getFormColumns($data);
- if (empty($columns) || !is_array($columns) ) {
- echo 'undefine form columns!';
- }
- else {
-
- // Generate the prefix of this module
- 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();
-
- // Initialize Form configuration
- // Customize submitted 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';
- }
-
- // Do you need to upload? if (!isset($isUpload)) $isUpload = true;
-
- // Customize the text of the submit button
- if (!isset($submitText)) $submitText = 'Submit';
-
- // Customize the width of the label part
- if (!isset($headWidth)) $headWidth = 130;
- $ headWidth = is_numeric($headWidth) && $headWidth > 0 ? $headWidth : 120;
-
- if (!isset($continueForm)) $continueForm = false;
-
-
- // Overloading
- 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);
- }
- }
- }
复制代码
|