DBO Form Widget

Jul 25, 2016 am 09:09 AM

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.
  1. //Data of the object to be operated
  2. // $target must be an instance based on MST_DBO
  3. if (!isset($data) || !is_object($data) || !$data instanceof MST_DBO) {
  4. echo '$data not a MST_DBO instance!';
  5. }
  6. else {
  7. // Get the associated module
  8. $model = get_class($data);
  9. // Define $columns
  10. // If not Definition, taken by default according to the MST_DBO interface
  11. if (!isset($columns))
  12. $columns = $data->getFormColumns($data);
  13. if (empty($columns) || !is_array($columns) ) {
  14. echo 'undefine form columns!';
  15. }
  16. else {
  17. // Generate the prefix of this module
  18. if (!isset($prefix))
  19. $prefix = strtolower($model);
  20. else
  21. $prefix = MST_String::tableize($prefix);
  22. if (!isset($id))
  23. $id = $prefix . '_form';
  24. if (!isset($class))
  25. $class = $prefix . '-form';
  26. $errors = $data->getErrors();
  27. // Initialize Form configuration
  28. // Customize submitted action
  29. if (!isset($action))
  30. $action = $this- >params->uri;
  31. // method
  32. if (!isset($method))
  33. $method = 'post';
  34. else {
  35. $method = strtolower((string)$method);
  36. if ( $method != 'get' && $method != 'post')
  37. $method = 'post';
  38. }
  39. // Do you need to upload? if (!isset($isUpload)) $isUpload = true;
  40. // Customize the text of the submit button
  41. if (!isset($submitText)) $submitText = 'Submit';
  42. // Customize the width of the label part
  43. if (!isset($headWidth)) $headWidth = 130;
  44. $ headWidth = is_numeric($headWidth) && $headWidth > 0 ? $headWidth : 120;
  45. if (!isset($continueForm)) $continueForm = false;
  46. // Overloading
  47. if (!isset($lineStart )) $lineStart = 1;
  48. ?>
  49. >
  50. < ?php } ?>
  51. $lineNum = $lineStart;
  52. $tinymceTimes = 0;
  53. foreach ($columns as $key => $column) {
  54. // Skip $column[0] as Empty part
  55. if (!isset($column[0])) continue;
  56. // Customize a row of common $column variables
  57. $key = strtolower(trim($key));
  58. $columnId = $prefix . ' _' . $key;
  59. $columnName = $prefix . '[' . $key . ']';
  60. $columnClass = $prefix . '-' . $key;
  61. $columnText = empty($column['title' ]) ? ucfirst($key) : $column['title'];
  62. $columnValue = isset($data[$key]) ? (string)$data[$key] : (isset($column['default' ]) ? $column['default'] : null);
  63. if (isset($column['forceValue'])) $columnValue = (string)$column['forceValue'];
  64. $isDisabled = isset($column ['disabled']) ? $column['disabled'] : false;
  65. $isReadonly = isset($column['readonly']) ? $column['readonly'] : false;
  66. if (is_object($column [0]) && $column[0] instanceof Closure) {
  67. $editType = 'closure';
  68. }
  69. else {
  70. $editType = strtolower($column[0]);
  71. }
  72. ?>
  73. if ($editType == 'dbo_form') {
  74. if (empty($column[1])) {
  75. // Missing dbo_form reuse instructions
  76. ?>
  77. } else {
  78. $column[1]['continueForm '] = true;
  79. $this->widget('base/dbo_form', $column[1]);
  80. }
  81. ?>
  82. $lineClass = $lineNum % 2 == 0 ? 'f-line f-line-odd' : 'f-line f-line-even';
  83. ?>
  84. }
  85. ?>
  86. Can't reuse dbo_form, please set the dbo_form options in $column[1].
  87. widget($column[ 1], $column[2]); ?>
  88. // 開始生成form行
  89. switch ($editType) {
  90. case 'text' :
  91. ?>
  92. if (isset($column['max']))
  93. echo ' maxlength="',$column['max'],'"';
  94. if ($isDisabled)
  95. echo ' disabled="disabled"';
  96. if ($isReadonly)
  97. echo ' readonly="readonly"';
  98. ?>/>
  99. break;
  100. case 'smalltextarea' :
  101. ?>
  102. break;
  103. case 'longtext' :
  104. ?>
  105. if (isset($column['max']))
  106. echo ' maxlength="',$column['max'],'"';
  107. if ($isDisabled)
  108. echo ' disabled="disabled"';
  109. if ($isReadonly)
  110. echo ' readonly="readonly"';
  111. ?>/>
  112. break;
  113. case 'upload' :
  114. ?>
  115. break;
  116. case 'password' :
  117. ?>
  118. if (isset($column['max']))
  119. echo ' maxlength="'.$column['max'].'"';
  120. if ($isDisabled)
  121. echo ' disabled="disabled"';
  122. if ($isReadonly)
  123. echo ' readonly="readonly"';
  124. ?>/>
  125. break;
  126. case 'textarea' :
  127. ?>
  128. break;
  129. case 'select' :
  130. $options = isset($column['options']) ? $column['options'] : array();
  131. $optionsType = isset($column['optionsType']) ? $column['optionsType'] : 'map';
  132. // 向前兼容:0 -> map, 1 -> list
  133. if (is_numeric($optionsType)) $optionsType = $optionsType > 0 ? 'list' : 'map';
  134. ?>
  135. break;
  136. case 'radiogroup' :
  137. $maps = isset($column['maps']) ? $column['maps'] : array();
  138. $mapsType = isset($column['mapsType']) ? $column['mapsType'] : 'map';
  139. // 向前兼容:0 -> map, 1 -> list
  140. if (is_numeric($mapsType)) $mapsType = $mapsType > 0 ? 'list' : 'map';
  141. ?>
  142. $index = 0;
  143. foreach ($maps as $key => $map) {
  144. $index++;
  145. ?>
  146. " class="f-radio" value="" />
  147. " class="f-radio" value="" />
  148. }
  149. ?>
  150. break;
  151. case 'datetime':
  152. $format = empty($column['dateFormat']) ? 'Y-m-d H:i:s' : $column['dateFormat'];
  153. $jsFormat = empty($column['jsFormat']) ? '%Y-%m-%d %H:00' : $column['jsFormat'];
  154. $pickSize = isset($column['pickSize']) ? intval($column['pickSize']) : 2;
  155. if ($columnValue > 0) {
  156. if (is_numeric($columnValue)) {
  157. $dtStr = date($format, $columnValue);
  158. $dtVal = $columnValue;
  159. }
  160. else {
  161. $dtStr = $columnValue;
  162. $dtVal = MST_String::date2num($columnValue);
  163. }
  164. }
  165. else {
  166. $dtStr = 0;
  167. $dtVal = 0;
  168. }
  169. if (is_numeric($dtVal) && $dtVal > 0)
  170. $dtStr = date($format, $dtVal);
  171. ?>
  172. type="text"
  173. id="_str"
  174. class="f-text f-dt "
  175. value=""
  176. />
  177. type="hidden"
  178. name=""
  179. id=""
  180. value=""
  181. />
  182. break;
  183. case 'tinymce' :
  184. $tinymceTimes++;
  185. ?>
  186. script('tiny_mce/tiny_mce.js'); ?>
  187. break;
  188. case 'widget' :
  189. $this->widget($column[1], $column[2]);
  190. break;
  191. }
  192. $lineNum++;
  193. ?>
  194. $index = 0;
  195. foreach ($submitText as $btnId => $btnText) {
  196. $index++;
  197. ?>
  198. validCode($prefix, 'input'); ?>
  199. }
  200. }
  201. ?>
复制代码
  1. $this->widget('base/dbo_form', array(
  2. 'data' => $this->list,
  3. ));
复制代码
  1. class Testimonial extends MST_DBO {
  2. protected static
  3. $columns = array(
  4. 'firstname' => array('text','title' => 'First Name', 'require' => 1, 'min' => 1, 'max' => 32),
  5. 'lastname' => array('text','title' => 'Last Name', 'require' => 1, 'min' => 1, 'max' => 32),
  6. 'avator' => array('title' => 'Avator', 'max' => 256),
  7. 'age_group' => array('title' => 'Age Group', 'require' => 1),
  8. 'secret' => array('textarea','title' => 'Secret', 'require' => 1, 'min' => 10, 'max' => 600),
  9. );
  10. public function getFormColumns() {
  11. if (GB_PERSSIONS == Region::ROOT) {
  12. $columns['region_id'] = array(
  13. 'select',
  14. 'title' => ' region ',
  15. 'optionsType' => 'list',
  16. 'options' => Region::find('all', array('select' => 'id, name')),
  17. );
  18. }
  19. else {
  20. $columns['region_id'] = array(
  21. 'hidden',
  22. 'default' => GB_PERSSIONS,
  23. );
  24. }
  25. $columns = array_merge($columns,self::$columns);
  26. $columns['age_group'] = array('widget', 'base/age_group', array(
  27. 'prefix' => 'testimonial',
  28. ), 'title' => 'Age Group');
  29. $columns['avator'] = array('widget', 'base/testmonial_upload', array(
  30. 'prefix' => 'testimonial',
  31. ), 'title' => 'Avator');
  32. return $columns;
  33. }
  34. public function beforeCreate(& $data) {
  35. $data['created_at'] = time();
  36. }
  37. public function getAge() {
  38. $ageGroup = array(
  39. 0 => '--',
  40. 1 => 'Under 18',
  41. 2 => '19 ? 25',
  42. 3 => '26 ? 35',
  43. 4 => '36 ? 45',
  44. 5 => '46 ? 55',
  45. 6 => '56 or above',
  46. );
  47. return isset($ageGroup[$this['age_group']]) ? $ageGroup[$this['age_group']] : $ageGroup[0];
  48. }
  49. public function getAvator() {
  50. return empty($this['avator']) ? httpUri('images/avator.png') : httpUri($this['avator']);
  51. }
  52. // 这是对MST_DBO的find的方法的重载
  53. static public function find($args = array(), $params = null, $isArray = false) {
  54. if (defined('GB_PERSSIONS') && GB_PERSSIONS == Region::ROOT) {
  55. self::initFind($args, $params, $isArray);
  56. return parent::find($args, $params, $isArray);
  57. }
  58. else {
  59. self::initFind($args, $params, $isArray);
  60. if (isset($args['where'])) {
  61. $args['where'][0] .= ' AND region_id = ?';
  62. $args['where'][] = GB_PERSSIONS;
  63. }
  64. else {
  65. $args['where'] = array('region_id = ?', GB_PERSSIONS);
  66. }
  67. return parent::find($args, $params, $isArray);
  68. }
  69. }
  70. }
复制代码
DBO Form Widget


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles