| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Module\Article\AdminControllers\Helper;
- use App\Module\Article\Enums\STATUS;
- use App\Module\Article\Models\ArticleCate;
- use Dcat\Admin\Form;
- use Dcat\Admin\Form\Field;
- /**
- * 表单辅助特性
- *
- * 提供文章模块后台控制器的表单构建功能的具体实现
- */
- trait FormHelperTrait
- {
- /**
- * 添加文章标题输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Text
- */
- public function textTitle(string $field = 'title', string $label = '标题'): Field\Text
- {
- return $this->form->text($field, $label)
- ->required()
- ->rules('required|max:200');
- }
- /**
- * 添加文章描述输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Textarea
- */
- public function textareaDescription(string $field = 'description', string $label = '简述'): Field\Textarea
- {
- return $this->form->textarea($field, $label)
- ->rows(3)
- ->rules('max:200');
- }
- /**
- * 添加文章分类选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select|Field\Radio
- */
- public function selectCategory(string $field = 'category_id', string $label = '分类', bool $useRadio = true)
- {
- $options = ArticleCate::getCate();
- if ($useRadio) {
- return $this->form->radio($field, $label)
- ->options($options)
- ->required();
- }
- return $this->form->select($field, $label)
- ->options($options)
- ->required();
- }
- /**
- * 添加文章状态选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select|Field\Radio
- */
- public function selectStatus(string $field = 'status', string $label = '状态', bool $useRadio = true)
- {
- $options = STATUS::getValueDescription();
- if ($useRadio) {
- return $this->form->radio($field, $label)
- ->options($options)
- ->default(STATUS::SHOW->value);
- }
- return $this->form->select($field, $label)
- ->options($options)
- ->default(STATUS::SHOW->value);
- }
- /**
- * 添加文章内容编辑器
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Editor
- */
- public function editorContent(string $field = 'content', string $label = '内容'): Field\Editor
- {
- return $this->form->editor($field, $label)->required();
- }
- /**
- * 添加文章排序输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Number
- */
- public function numberSortOrder(string $field = 'sort_order', string $label = '排序'): Field\Number
- {
- return $this->form->number($field, $label)
- ->default(0)
- ->help('数字越小越靠前');
- }
- /**
- * 添加文章是否置顶开关
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Switch
- */
- public function switchIsTop(string $field = 'is_top', string $label = '是否置顶'): Field\Switch
- {
- return $this->form->switch($field, $label)->default(0);
- }
- /**
- * 添加文章是否推荐开关
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Switch
- */
- public function switchIsRecommend(string $field = 'is_recommend', string $label = '是否推荐'): Field\Switch
- {
- return $this->form->switch($field, $label)->default(0);
- }
- /**
- * 添加文章创建者隐藏输入
- *
- * @param string $field 字段名
- * @param int|null $userId 用户ID
- * @return Field\Hidden
- */
- public function hiddenCreatedBy(string $field = 'created_by', ?int $userId = null): Field\Hidden
- {
- $value = $userId ?? \Dcat\Admin\Admin::user()->getOriginal('id');
- return $this->form->hidden($field)->value($value);
- }
- }
|