BaseComponent.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\form;
  12. /**
  13. * 基础组件集成
  14. * Class BaseComponent
  15. * @package crmeb\form
  16. */
  17. abstract class BaseComponent
  18. {
  19. /**
  20. * @var bool
  21. */
  22. protected $init = false;
  23. /**
  24. * @var array
  25. */
  26. protected $rule = [];
  27. /**
  28. * 数据库验证
  29. * @var array
  30. */
  31. protected $validate = [];
  32. /**
  33. * @var CommonRule
  34. */
  35. protected $validataRule;
  36. /**
  37. * 是否实例化
  38. */
  39. protected function init()
  40. {
  41. if (!$this->init) {
  42. $this->validataRule = new CommonRule;
  43. $this->init = true;
  44. }
  45. }
  46. /**
  47. * 多个验证规则
  48. * @param array $validate
  49. * @return $this
  50. */
  51. public function validates(array $validate)
  52. {
  53. $this->validate = $validate;
  54. return $this;
  55. }
  56. /**
  57. * 单个验证规则
  58. * @param CommonRule $validate
  59. * @return $this
  60. */
  61. public function validate(CommonRule $validate)
  62. {
  63. $this->validate[] = $validate;
  64. return $this;
  65. }
  66. /**
  67. * 是否必填
  68. * @return $this
  69. */
  70. public function required()
  71. {
  72. $this->init();
  73. $this->validataRule->required();
  74. return $this;
  75. }
  76. /**
  77. * 设置提示消息
  78. * @param string $message
  79. * @return $this
  80. */
  81. public function message(string $message)
  82. {
  83. $this->init();
  84. $this->validataRule->message($message);
  85. return $this;
  86. }
  87. /**
  88. * 数据写入
  89. */
  90. protected function before()
  91. {
  92. if (!$this->validate && $this->validataRule instanceof CommonRule) {
  93. if (!$this->validataRule->getMessage() && $this->rule['title']) {
  94. $this->validataRule->message('请输入' . $this->rule['title']);
  95. }
  96. $this->validate[] = $this->validataRule->toArray();
  97. }
  98. $validate = [];
  99. foreach ($this->validate as $item) {
  100. if ($item instanceof CommonRule) {
  101. $validate[] = $item->toArray();
  102. } else {
  103. $validate[] = $item;
  104. }
  105. }
  106. $this->rule['validate'] = $validate;
  107. }
  108. }