LotteryEventsForms.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Cas\Dao;
  3. use KIF\Dao\DBAgileDev;
  4. use KIF\Data\ResultWrapper;
  5. /**
  6. *
  7. * 活动表单数据
  8. */
  9. class LotteryEventsForms extends DBAgileDev {
  10. protected $tableName = 'lottery_events_forms';
  11. /**
  12. * 数据库里的真实字段
  13. * @var array
  14. */
  15. protected $other_field = array(
  16. 'events_id',
  17. 'uid',
  18. # 审核字段 1未审核 2审核通过 3审核不通过
  19. 'audit',
  20. );
  21. /**
  22. *
  23. * 验证表单数据
  24. * @param array $forms 后台定义的表单
  25. * @param array $formData 用户提交的表单数据
  26. * @return Ambigous <\KIF\Data\ResultWrapper, \KIF\Data\ResultWrapper>array
  27. */
  28. public function verifyFormData($forms, $formData) {
  29. if (!$forms) {
  30. return ResultWrapper::success();
  31. }
  32. foreach ($forms as $tmpForm) {
  33. $data = $formData[$tmpForm['name']];
  34. if ($tmpForm['required']) {
  35. if (!$data) {
  36. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  37. return ResultWrapper::fail($errorMsg);
  38. }
  39. if ($tmpForm['rule']['size']) {
  40. if (mb_strlen($data) < $tmpForm['rule']['size']) {
  41. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  42. return ResultWrapper::fail($errorMsg);
  43. }
  44. }
  45. if ($tmpForm['rule']['regular']) {
  46. if (!preg_match($tmpForm['rule']['regular'], $data)) {
  47. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  48. return ResultWrapper::fail($errorMsg);
  49. }
  50. }
  51. } else {
  52. if (!$data) {
  53. continue;
  54. }
  55. if ($tmpForm['rule']['size']) {
  56. if (mb_strlen($data) < $tmpForm['rule']['size']) {
  57. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  58. return ResultWrapper::fail($errorMsg);
  59. }
  60. }
  61. if ($tmpForm['rule']['regular']) {
  62. if (!preg_match($tmpForm['rule']['regular'], $data)) {
  63. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  64. return ResultWrapper::fail($errorMsg);
  65. }
  66. }
  67. }
  68. }
  69. return ResultWrapper::success();
  70. }
  71. /**
  72. *
  73. * 获取表单验证的错误信息
  74. * @param unknown $form_name
  75. * @param string $form_type
  76. * @return boolean|string
  77. */
  78. public function getErrorMsg($form_name, $form_type = 'text') {
  79. if (!$form_name) {
  80. return false;
  81. }
  82. $errorMsg = '';
  83. switch ($form_type) {
  84. case 'text':
  85. $errorMsg = "请填写有效{$form_name}";
  86. break;
  87. case 'textarea':
  88. $errorMsg = "请填写有效{$form_name}";
  89. break;
  90. case 'select':
  91. $errorMsg = "请选择{$form_name}";
  92. break;
  93. default:
  94. $errorMsg = "";
  95. break;
  96. }
  97. return $errorMsg;
  98. }
  99. }