Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 2 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace crmeb\form;
  3. /**
  4. * 基础组件集成
  5. * Class BaseComponent
  6. */
  7. abstract class BaseComponent
  8. {
  9. /**
  10. * @var bool
  11. */
  12. protected $init = false;
  13. /**
  14. * @var array
  15. */
  16. protected $rule = [];
  17. /**
  18. * 数据库验证
  19. * @var array
  20. */
  21. protected $validate = [];
  22. /**
  23. * @var CommonRule
  24. */
  25. protected $validataRule;
  26. /**
  27. * 是否实例化
  28. */
  29. protected function init()
  30. {
  31. if (!$this->init) {
  32. $this->validataRule = new CommonRule;
  33. $this->init = true;
  34. }
  35. }
  36. /**
  37. * 多个验证规则
  38. * @param array $validate
  39. * @return $this
  40. */
  41. public function validates(array $validate)
  42. {
  43. $this->validate = $validate;
  44. return $this;
  45. }
  46. /**
  47. * 单个验证规则
  48. * @param CommonRule $validate
  49. * @return $this
  50. */
  51. public function validate(CommonRule $validate)
  52. {
  53. $this->validate[] = $validate;
  54. return $this;
  55. }
  56. /**
  57. * 是否必填
  58. * @return $this
  59. */
  60. public function required()
  61. {
  62. $this->init();
  63. $this->validataRule->required();
  64. return $this;
  65. }
  66. /**
  67. * 设置提示消息
  68. * @param string $message
  69. * @return $this
  70. */
  71. public function message(string $message)
  72. {
  73. $this->init();
  74. $this->validataRule->message($message);
  75. return $this;
  76. }
  77. /**
  78. * 数据写入
  79. */
  80. protected function before()
  81. {
  82. if (!$this->validate && $this->validataRule instanceof CommonRule) {
  83. if (!$this->validataRule->getMessage() && $this->rule['title']) {
  84. $this->validataRule->message('请输入' . $this->rule['title']);
  85. }
  86. $this->validate[] = $this->validataRule->toArray();
  87. }
  88. $validate = [];
  89. foreach ($this->validate as $item) {
  90. if ($item instanceof CommonRule) {
  91. $validate[] = $item->toArray();
  92. } else {
  93. $validate[] = $item;
  94. }
  95. }
  96. $this->rule['validate'] = $validate;
  97. }
  98. }