DBO Form Widget

Source: Internet
Author: User
This is a very useful component that can be used to generate an edit form of the data, which is a very important component of the MST Library 3.1, which enables the loop nesting of the dbo form and the dbo form, is controlled in a form, and supports the DBO The custom widget component is nested again in the form.
Many PHP frameworks, which write the form generation in the function, are understandable, but you cannot receive the performance cost of iterating over the user function when generating a form, and building a PHP and HTML-mixed code is much better than the performance of the loop execution function.
And, most of the time, we know the structure of a data object, we have defined the value type of each field of the data object, we really do not need to manually write what form, or to use the code to judge, this with select, that with textarea or something.
  1. The image of the pair to manipulate
  2. $target must be based on Mst_dbo's example
  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 Contact module
  8. $model = Get_class ($data);
  9. Definition $columns
  10. If undefined, the Mst_dbo interface is taken
  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 front able of the 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. Initializing the form configuration
  28. Customizing the 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. Is it necessary to upload the
  40. if (!isset ($isUpload)) $isUpload = true;
  41. Customizing the text of the Submit button
  42. if (!isset ($submitText)) $submitText = ' Submit ';
  43. Customizing the width of the label part
  44. if (!isset ($headWidth)) $headWidth = 130;
  45. $headWidth = Is_numeric ($headWidth) && $headWidth > 0? $headWidth: 120;
  46. if (!isset ($continueForm)) $continueForm = false;
  47. Re-download
  48. if (!isset ($lineStart)) $lineStart = 1;
  49. ?>
  50. }
  51. }
  52. ?>
Copy Code
    1. $this->widget (' Base/dbo_form ', Array (
    2. ' Data ' = $this->list,
    3. ));
Copy Code
  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 ' = +, ' 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 = ' 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. This is an overload of the method of find for Mst_dbo
  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. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.