FormHelperTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Module\Article\AdminControllers\Helper;
  3. use App\Module\Article\Enums\STATUS;
  4. use App\Module\Article\Models\ArticleCate;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Form\Field;
  7. /**
  8. * 表单辅助特性
  9. *
  10. * 提供文章模块后台控制器的表单构建功能的具体实现
  11. */
  12. trait FormHelperTrait
  13. {
  14. /**
  15. * 添加文章标题输入
  16. *
  17. * @param string $field 字段名
  18. * @param string $label 标签名
  19. * @return Field\Text
  20. */
  21. public function textTitle(string $field = 'title', string $label = '标题'): Field\Text
  22. {
  23. return $this->form->text($field, $label)
  24. ->required()
  25. ->rules('required|max:200');
  26. }
  27. /**
  28. * 添加文章描述输入
  29. *
  30. * @param string $field 字段名
  31. * @param string $label 标签名
  32. * @return Field\Textarea
  33. */
  34. public function textareaDescription(string $field = 'description', string $label = '简述'): Field\Textarea
  35. {
  36. return $this->form->textarea($field, $label)
  37. ->rows(3)
  38. ->rules('max:200');
  39. }
  40. /**
  41. * 添加文章分类选择
  42. *
  43. * @param string $field 字段名
  44. * @param string $label 标签名
  45. * @return Field\Select|Field\Radio
  46. */
  47. public function selectCategory(string $field = 'category_id', string $label = '分类', bool $useRadio = true)
  48. {
  49. $options = ArticleCate::getCate();
  50. if ($useRadio) {
  51. return $this->form->radio($field, $label)
  52. ->options($options)
  53. ->required();
  54. }
  55. return $this->form->select($field, $label)
  56. ->options($options)
  57. ->required();
  58. }
  59. /**
  60. * 添加文章状态选择
  61. *
  62. * @param string $field 字段名
  63. * @param string $label 标签名
  64. * @return Field\Select|Field\Radio
  65. */
  66. public function selectStatus(string $field = 'status', string $label = '状态', bool $useRadio = true)
  67. {
  68. $options = STATUS::getValueDescription();
  69. if ($useRadio) {
  70. return $this->form->radio($field, $label)
  71. ->options($options)
  72. ->default(STATUS::SHOW->value);
  73. }
  74. return $this->form->select($field, $label)
  75. ->options($options)
  76. ->default(STATUS::SHOW->value);
  77. }
  78. /**
  79. * 添加文章内容编辑器
  80. *
  81. * @param string $field 字段名
  82. * @param string $label 标签名
  83. * @return Field\Editor
  84. */
  85. public function editorContent(string $field = 'content', string $label = '内容'): Field\Editor
  86. {
  87. return $this->form->editor($field, $label)->required();
  88. }
  89. /**
  90. * 添加文章排序输入
  91. *
  92. * @param string $field 字段名
  93. * @param string $label 标签名
  94. * @return Field\Number
  95. */
  96. public function numberSortOrder(string $field = 'sort_order', string $label = '排序'): Field\Number
  97. {
  98. return $this->form->number($field, $label)
  99. ->default(0)
  100. ->help('数字越小越靠前');
  101. }
  102. /**
  103. * 添加文章是否置顶开关
  104. *
  105. * @param string $field 字段名
  106. * @param string $label 标签名
  107. * @return Field\Switch
  108. */
  109. public function switchIsTop(string $field = 'is_top', string $label = '是否置顶'): Field\Switch
  110. {
  111. return $this->form->switch($field, $label)->default(0);
  112. }
  113. /**
  114. * 添加文章是否推荐开关
  115. *
  116. * @param string $field 字段名
  117. * @param string $label 标签名
  118. * @return Field\Switch
  119. */
  120. public function switchIsRecommend(string $field = 'is_recommend', string $label = '是否推荐'): Field\Switch
  121. {
  122. return $this->form->switch($field, $label)->default(0);
  123. }
  124. /**
  125. * 添加文章创建者隐藏输入
  126. *
  127. * @param string $field 字段名
  128. * @param int|null $userId 用户ID
  129. * @return Field\Hidden
  130. */
  131. public function hiddenCreatedBy(string $field = 'created_by', ?int $userId = null): Field\Hidden
  132. {
  133. $value = $userId ?? \Dcat\Admin\Admin::user()->getOriginal('id');
  134. return $this->form->hidden($field)->value($value);
  135. }
  136. }