Form toolkit with synchronization token

Source: Internet
Author: User
Form toolkit with synchronization token
It is said that the Shenzhen 2011 Universiade is not coming, so I was arrested by the Youth League Committee to serve as a volunteer service station. A small form toolkit is written in the process. The verification function is not implemented by myself. it depends on Kohana_Validate (Kohana V3.0x branch ). However, according to The J2EE disgusting mode, I got a synchronization token to prevent repeated forms from being submitted. This component also sets the abstraction layer. I think the verification code can also be integrated as a token subclass. But now there is no time to get it done. first, share the code and wait for it to be made ~

  1. Namespace Form;
  2. Use Volunteer \ Form \ AbstractForm;
  3. Class NewsPoster extends AbstractForm
  4. {
  5. /**
  6. * Add all preset form elements
  7. *
  8. * @ Abstract
  9. */
  10. Public function bindAllElement ()
  11. {
  12. $ This-> bindToken ('valid _ token ')
  13. -> AddElement ('news title', 'title ',
  14. Array (
  15. 'Not _ empty' => null,
  16. 'Max _ length' => array (60)
  17. ),
  18. Array (
  19. 'Trim' => null,
  20. 'Htmlspecialchars' => array (ENT_QUOTES ),
  21. ))
  22. -> AddElement ('thumbnail url ', 'urlname ',
  23. Array (
  24. 'Max _ length' => array (50 ),
  25. 'Regex' => array ('~ ^ [A-zA-Z0-9 \-%] + $ ~ ')
  26. ),
  27. Array (
  28. 'Trim' => null,
  29. 'Urlencode' => null,
  30. ))
  31. -> AddElement ('keyword', 'keyword ',
  32. Array (
  33. 'Max _ length' => array (100 ),
  34. ),
  35. Array (
  36. 'Trim' => null,
  37. 'Htmlspecialchars' => array (ENT_QUOTES ),
  38. ))
  39. -> AddElement ('author & reporter ', 'author ',
  40. Array (
  41. 'Max _ length' => array (60 ),
  42. ),
  43. Array (
  44. 'Trim' => null,
  45. 'Htmlspecialchars' => array (ENT_QUOTES ),
  46. ))
  47. -> AddElement ('news editor', 'editor ',
  48. Array (
  49. 'Max _ length' => array (60 ),
  50. ),
  51. Array (
  52. 'Trim' => null,
  53. 'Htmlspecialchars' => array (ENT_QUOTES ),
  54. ))
  55. -> AddElement ('news source', 'source ',
  56. Array (
  57. 'Max _ length' => array (60 ),
  58. ),
  59. Array (
  60. 'Trim' => null,
  61. 'Htmlspecialchars' => array (ENT_QUOTES ),
  62. ))
  63. -> AddElement ('news category', 'Category ',
  64. Array (
  65. 'Not _ empty' => null,
  66. 'Digit' => null,
  67. 'Regex' => array ('~ ^ [1-9] \ d * $ ~ ')
  68. ),
  69. Array ())
  70. -> AddElement ('cover image', 'cover _ image ',
  71. Array (),
  72. Array (
  73. 'Trim' => null,
  74. 'Urlencode' => null,
  75. ))
  76. -> AddElement ('containing media information', 'mediatag ',
  77. Array (
  78. 'Not _ empty' => null,
  79. 'Digit' => null,
  80. 'Regex' => array ('~ ^ [1, 0123] $ ~ ')
  81. ),
  82. Array ())
  83. -> AddElement ('news summary', 'summary ',
  84. Array (
  85. 'Max _ length' => array (255 ),
  86. ),
  87. Array (
  88. 'Trim' => null,
  89. 'Htmlspecialchars' => array (ENT_QUOTES ),
  90. ))
  91. -> AddElement ('publication status', 'State ',
  92. Array (
  93. 'Not _ empty' => null,
  94. 'Digit' => null,
  95. 'Regex' => array ('~ ^ [01] $ ~ ')
  96. ),
  97. Array ())
  98. -> AddElement ('Show on Homepage? ', 'showinhome ',
  99. Array (
  100. 'Boolean' => null,
  101. ),
  102. Array ())
  103. -> AddElement ('news content', 'content ',
  104. Array (
  105. 'Not _ empty' => null,
  106. ),
  107. Array (
  108. 'Tidy _ parse_string '=> array (
  109. Array (
  110. 'Indent '=> true,
  111. 'Output-xhtml '=> true,
  112. 'Clean' => true,
  113. 'Drop-font-tags' => true,
  114. 'Show-body-only' => true
  115. ), 'Utf8 '),
  116. 'Trim' => null
  117. ));
  118. }
  119. }

  1. Public function action_GET ()
  2. {
  3. // Obtain the ID in the url
  4. $ Id = $ this-> request-> param ('id', null );
  5. // Send the token to the view layer (put it in the hidden field of the form)
  6. $ This-> view ['token'] = Form \ NewsPoster: getToken ()-> useToken ();
  7. }
  8. Public function action_POST ()
  9. {
  10. $ Id = $ this-> request-> param ('id', null );
  11. $ Form = Form \ NewsPoster: factory ($ _ POST );
  12. $ Form-> bindAllElement ();
  13. If ($ form-> checkForm ()){
  14. // After the verification is passed, call the domain model to perform the operation ~
  15. $ This-> view ['success'] = true;
  16. } Else {
  17. $ This-> view ['success'] = false;
  18. // Send the error message back to the view layer
  19. $ This-> view ['message'] = array_values ($ form-> getMessage ());
  20. }
  21. }

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.