LotteryEventsForms.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. );
  19. /**
  20. *
  21. * 验证表单数据
  22. * @param array $forms 后台定义的表单
  23. * @param array $formData 用户提交的表单数据
  24. * @return Ambigous <\KIF\Data\ResultWrapper, \KIF\Data\ResultWrapper>array
  25. */
  26. public function verifyFormData($forms, $formData) {
  27. if (!$forms) {
  28. return ResultWrapper::success();
  29. }
  30. foreach ($forms as $tmpForm) {
  31. $data = $formData[$tmpForm['name']];
  32. if ($tmpForm['required']) {
  33. if (!$data) {
  34. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  35. return ResultWrapper::fail($errorMsg);
  36. }
  37. if ($tmpForm['rule']['size']) {
  38. if (mb_strlen($data) < $tmpForm['rule']['size']) {
  39. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  40. return ResultWrapper::fail($errorMsg);
  41. }
  42. }
  43. if ($tmpForm['rule']['regular']) {
  44. if (!preg_match($tmpForm['rule']['regular'], $data)) {
  45. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  46. return ResultWrapper::fail($errorMsg);
  47. }
  48. }
  49. } else {
  50. if (!$data) {
  51. continue;
  52. }
  53. if ($tmpForm['rule']['size']) {
  54. if (mb_strlen($data) < $tmpForm['rule']['size']) {
  55. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  56. return ResultWrapper::fail($errorMsg);
  57. }
  58. }
  59. if ($tmpForm['rule']['regular']) {
  60. if (!preg_match($tmpForm['rule']['regular'], $data)) {
  61. $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']);
  62. return ResultWrapper::fail($errorMsg);
  63. }
  64. }
  65. }
  66. }
  67. return ResultWrapper::success();
  68. }
  69. /**
  70. *
  71. * 获取表单验证的错误信息
  72. * @param unknown $form_name
  73. * @param string $form_type
  74. * @return boolean|string
  75. */
  76. public function getErrorMsg($form_name, $form_type = 'text') {
  77. if (!$form_name) {
  78. return false;
  79. }
  80. $errorMsg = '';
  81. switch ($form_type) {
  82. case 'text':
  83. $errorMsg = "请填写有效{$form_name}";
  84. break;
  85. case 'textarea':
  86. $errorMsg = "请填写有效{$form_name}";
  87. break;
  88. case 'select':
  89. $errorMsg = "请选择{$form_name}";
  90. break;
  91. default:
  92. $errorMsg = "";
  93. break;
  94. }
  95. return $errorMsg;
  96. }
  97. }