工具表单

工具表单(Dcat\Admin\Widgets\Form)用来构建表单和处理提交数据,可以很方便的独立处理数据,而不需要额外注册路由。

基本使用

用命令admin:form来生成表单类文件:

  1. php artisan admin:form Setting

将会生成表单文件app/Admin/Forms/Setting.php

  1. <?php
  2. namespace App\Admin\Forms;
  3. use Dcat\Admin\Widgets\Form;
  4. use Symfony\Component\HttpFoundation\Response;
  5. class Setting extends Form
  6. {
  7. // 处理表单提交请求
  8. public function handle(array $input)
  9. {
  10. // dump($input);
  11. // return $this->error('Your error message.');
  12. return $this->success('Processed successfully.', '/');
  13. }
  14. // 构建表单
  15. public function form()
  16. {
  17. // Since v1.6.5 弹出确认弹窗
  18. $this->confirm('您确定要提交表单吗', 'content');
  19. $this->text('name')->required();
  20. $this->email('email')->rules('email');
  21. }
  22. /**
  23. * 返回表单数据,如不需要可以删除此方法
  24. *
  25. * @return array
  26. */
  27. public function default()
  28. {
  29. return [
  30. 'name' => 'John Doe',
  31. 'email' => 'John.Doe@gmail.com',
  32. ];
  33. }
  34. }

在上面的表单类里面,在form方法中构建表单项,使用方法和数据表单一致,default方法用来给这个表单项设置默认数据。

在页面中填入数据提交表单之后,请求会进入到handle方法中,在这里可以加入你的数据处理逻辑,处理完成之后可以通过successerror方法响应数据到前端:

  1. public function handle(array $input)
  2. {
  3. // $input是你接收到的表单数据
  4. // 在这里可以写你的处理逻辑
  5. // 第一个参数是响应的成功信息,第二个参数是要跳转的路由
  6. return $this->success('Processed successfully.', '/');
  7. }

然后按照下面的方法将上面的表单放到你的页面中:

  1. <?php
  2. use App\Admin\Forms\Setting;
  3. use App\Http\Controllers\Controller;
  4. use Dcat\Admin\Widgets\Card;
  5. use Dcat\Admin\Layout\Content;
  6. class UserController extends Controller
  7. {
  8. public function setting(Content $content)
  9. {
  10. return $content
  11. ->title('网站设置')
  12. ->body(new Card(new Setting()));
  13. }
  14. }

弹出确认弹窗

{tip} Since v1.6.5

第二个参数可忽略

  1. $this->confirm('title', 'content');

响应方法

ajaxResponse 仅提示成功或失败信息,不跳转也不刷新页面

  1. public function handle(array $input)
  2. {
  3. ...
  4. return $this->ajaxResponse('操作失败', null, false);
  5. return $this->ajaxResponse('操作成功');
  6. }

success 响应成功信息,第一个参数为提示信息,第二个参数为跳转地址,不传则刷新当前页

  1. public function handle(array $input)
  2. {
  3. ...
  4. return $this->success('操作成功');
  5. return $this->success('操作成功', 'auth/users');
  6. }

error 响应事变信息,第一个参数为提示信息,第二个参数为跳转地址,不传则刷新当前页

  1. public function handle(array $input)
  2. {
  3. ...
  4. return $this->error('操作失败');
  5. return $this->error('操作失败', 'auth/users');
  6. }

{tip} Since v1.6.0

location 刷新整个页面

  1. public function handle(array $input)
  2. {
  3. ...
  4. // 不传参数则刷新当前页面
  5. return $this->location();
  6. return $this->location('auth/user', '保存成功');
  7. return $this->location('auth/user', [
  8. 'message' => '系统错误',
  9. 'status' => false,
  10. ]);
  11. }

自定义表单保存的后续行为

{tip} Since 1.2.0

  1. <?php
  2. namespace App\Admin\Forms;
  3. use Dcat\Admin\Widgets\Form;
  4. use Symfony\Component\HttpFoundation\Response;
  5. class Setting extends Form
  6. {
  7. ...
  8. /**
  9. * 设置表单保存成功后执行的JS
  10. *
  11. * v1.6.5 版本之前请用 buildSuccessScript 方法
  12. *
  13. * @return string|void
  14. */
  15. protected function addSavedScript()
  16. {
  17. return <<<JS
  18. // data 为接口返回数据
  19. if (! data.status) {
  20. Dcat.error(data.message);
  21. return false;
  22. }
  23. Dcat.success(data.message);
  24. if (data.redirect) {
  25. Dcat.reload(data.redirect)
  26. }
  27. // 中止后续逻辑(默认逻辑)
  28. return false;
  29. JS;
  30. }
  31. /**
  32. * 设置表单保存失败后执行的JS
  33. *
  34. * v1.6.5 版本之前请用 buildErrorScript 方法
  35. *
  36. * @return string|void
  37. */
  38. protected function addErrorScript()
  39. {
  40. return <<<JS
  41. var errorData = JSON.parse(response.responseText);
  42. if (errorData) {
  43. Dcat.error(errorData.message);
  44. } else {
  45. console.log('提交出错', response.responseText);
  46. }
  47. // 终止后续逻辑执行(默认逻辑)
  48. return false;
  49. JS;
  50. }
  51. }

取消表单的Ajax提交

默认表单提交都使用Ajax进行提交,通过注入的 Form.js 进行流程控制. 如果需要原生表单行为,可以禁用此特

  1. $form->disableAjaxSubmit();

布局

{tip} Since v1.6.0

column多列布局

  1. <?php
  2. use Dcat\Admin\Widgets\Form;
  3. class Setting extends Form
  4. {
  5. public function form()
  6. {
  7. $this->column(6, function () {
  8. $this->text('text1');
  9. ...
  10. });
  11. $this->column(6, function () {
  12. $this->text('text2');
  13. ...
  14. });
  15. }
  16. }

tab选项卡布局

  1. <?php
  2. use Dcat\Admin\Widgets\Form;
  3. class Setting extends Form
  4. {
  5. public function form()
  6. {
  7. $this->tab('选项卡1', function () {
  8. $this->text('text1');
  9. ...
  10. });
  11. $this->tab('选项卡2', function () {
  12. $this->text('text2');
  13. ...
  14. });
  15. }
  16. }

row多行布局

  1. public function form()
  2. {
  3. $this->row(function ($row) {
  4. $row->width(3)->text('text1');
  5. ...
  6. });
  7. $this->row(function ($row) {
  8. $row->width(3)->text('text2');
  9. ...
  10. });
  11. }

在弹窗中显示

{tip} Since v1.7.0

基本用法

使用命令生成工具表单php artisan admin:form ResetPassword,然后修改表单文件如下

  1. <?php
  2. namespace App\Admin\Forms;
  3. use Dcat\Admin\Widgets\Form;
  4. class ResetPassword extends Form
  5. {
  6. // 处理请求
  7. public function handle(array $input)
  8. {
  9. $password = $input['password'] ?? null;
  10. // 逻辑操作
  11. return $this->success('密码修改成功');
  12. }
  13. public function form()
  14. {
  15. $this->password('password')->required();
  16. // 密码确认表单
  17. $this->password('password_confirm')->same('password');
  18. }
  19. // 返回表单数据,如不需要可以删除此方法
  20. public function default()
  21. {
  22. return [
  23. 'password' => '',
  24. 'password_confirm' => '',
  25. ];
  26. }
  27. }

使用

  1. use App\Admin\Forms\ResetPassword;
  2. use Dcat\Admin\Widgets\Modal;
  3. $modal = Modal::make()
  4. ->lg()
  5. ->title('修改密码')
  6. ->body(ResetPassword::make())
  7. ->button('修改密码');

异步加载

只需要让Form表单类实现Dcat\Admin\Contracts\LazyRenderable接口即可支持异步渲染功能,修改上面创建的工具表单类如下

  1. <?php
  2. namespace App\Admin\Forms;
  3. use Dcat\Admin\Widgets\Form;
  4. use Dcat\Admin\Traits\LazyWidget;
  5. use Dcat\Admin\Contracts\LazyRenderable;
  6. class ResetPassword extends Form implements LazyRenderable
  7. {
  8. use LazyWidget;
  9. // 处理请求
  10. public function handle(array $input)
  11. {
  12. // 获取外部传递参数
  13. $id = $this->payload['id'] ?? null;
  14. $password = $input['password'] ?? null;
  15. // 逻辑操作
  16. return $this->success('密码修改成功');
  17. }
  18. public function form()
  19. {
  20. // 获取外部传递参数
  21. $id = $this->payload['id'] ?? null;
  22. $this->password('password')->required();
  23. // 密码确认表单
  24. $this->password('password_confirm')->same('password');
  25. }
  26. // 返回表单数据,如不需要可以删除此方法
  27. public function default()
  28. {
  29. // 获取外部传递参数
  30. $id = $this->payload['id'] ?? null;
  31. return [
  32. 'password' => '',
  33. 'password_confirm' => '',
  34. ];
  35. }
  36. }

使用代码与上面基本一致,并且我们可以用payload方法往表单里面传递自定义参数

  1. use App\Admin\Forms\ResetPassword;
  2. use Dcat\Admin\Widgets\Modal;
  3. $modal = Modal::make()
  4. ->lg()
  5. ->title('修改密码')
  6. ->body(ResetPassword::make()->payload(['id' => '...'])) // 传递自定义参数
  7. ->button('修改密码');

表格行操作弹窗

{tip} Since v1.7.0

下面通过一个数据表格修改密码的行操作功能来展示弹窗结合工具表单的用法:

使用命令生成工具表单php artisan admin:form ResetPassword,然后修改表单文件如下

  1. <?php
  2. namespace App\Admin\Forms;
  3. use Dcat\Admin\Models\Administrator;
  4. use Dcat\Admin\Traits\LazyWidget;
  5. use Dcat\Admin\Widgets\Form;
  6. use Dcat\Admin\Contracts\LazyRenderable;
  7. class ResetPassword extends Form implements LazyRenderable
  8. {
  9. use LazyWidget; // 使用异步加载功能
  10. // 处理请求
  11. public function handle(array $input)
  12. {
  13. // 获取外部传递参数
  14. $id = $this->payload['id'] ?? null;
  15. // 表单参数
  16. $password = $input['password'] ?? null;
  17. if (! $id) {
  18. return $this->error('参数错误');
  19. }
  20. $user = Administrator::query()->find($id);
  21. if (! $user) {
  22. return $this->error('用户不存在');
  23. }
  24. $user->update(['password' => bcrypt($password)]);
  25. return $this->success('密码修改成功');
  26. }
  27. public function form()
  28. {
  29. // 获取外部传递参数
  30. //$id = $this->payload['id'] ?? null;
  31. $this->password('password')->required();
  32. // 密码确认表单
  33. $this->password('password_confirm')->same('password');
  34. }
  35. // 返回表单数据,如不需要可以删除此方法
  36. public function default()
  37. {
  38. return [
  39. 'password' => '',
  40. 'password_confirm' => '',
  41. ];
  42. }
  43. }

然后运行php artisan admin:action命令,选择选项2,生成数据表格行操作类,并修改如下:

  1. <?php
  2. namespace App\Admin\Actions\Grid;
  3. use App\Admin\Forms\ResetPassword as ResetPasswordForm;
  4. use Dcat\Admin\Widgets\Modal;
  5. use Dcat\Admin\Grid\RowAction;
  6. class ResetPassword extends RowAction
  7. {
  8. protected $title = '修改密码';
  9. public function render()
  10. {
  11. // 实例化表单类并传递自定义参数
  12. $form = ResetPasswordForm::make()->payload(['id' => $this->getKey()]);
  13. return Modal::make()
  14. ->lg()
  15. ->title($this->title)
  16. ->body($form)
  17. ->button($this->title);
  18. }
  19. }

使用

  1. use App\Admin\Actions\Grid\ResetPassword;
  2. $grid->actions([new ResetPassword()]);

效果

工具表单 - 图1

表格批量操作弹窗

{tip} Since v1.7.0

如果你想在批量操作按钮中使用表单弹窗,可以参考以下例子:

这里我们仍然沿用上面用到的App\Admin\Forms\ResetPassword表单,并修改如下

  1. <?php
  2. namespace App\Admin\Forms;
  3. use Dcat\Admin\Models\Administrator;
  4. use Dcat\Admin\Widgets\Form;
  5. use Dcat\Admin\Traits\LazyWidget;
  6. use Dcat\Admin\Contracts\LazyRenderable;
  7. class ResetPassword extends Form implements LazyRenderable
  8. {
  9. use LazyWidget;
  10. // 处理请求
  11. public function handle(array $input)
  12. {
  13. // id转化为数组
  14. $id = explode(',', $input['id'] ?? null);
  15. $password = $input['password'] ?? null;
  16. if (! $id) {
  17. return $this->error('参数错误');
  18. }
  19. $users = Administrator::query()->find($id);
  20. if ($users->isEmpty()) {
  21. return $this->error('用户不存在');
  22. }
  23. // 这里改为循环批量修改
  24. $users->each(function ($user) use ($password) {
  25. $user->update(['password' => bcrypt($password)]);
  26. });
  27. return $this->success('密码修改成功');
  28. }
  29. public function form()
  30. {
  31. $this->password('password')->required();
  32. // 密码确认表单
  33. $this->password('password_confirm')->same('password');
  34. // 设置隐藏表单,传递用户id
  35. $this->hidden('id')->attribute('id', 'reset-password-id');
  36. }
  37. // 返回表单数据,如不需要可以删除此方法
  38. public function default()
  39. {
  40. return [
  41. 'password' => '',
  42. 'password_confirm' => '',
  43. ];
  44. }
  45. }

然后运行php artisan admin:action命令,选择选项1,生成数据表格批量操作类,并修改如下:

  1. <?php
  2. namespace App\Admin\Actions\Grid;
  3. use App\Admin\Forms\ResetPassword as ResetPasswordForm;
  4. use Dcat\Admin\Widgets\Modal;
  5. use Dcat\Admin\Grid\BatchAction;
  6. class BatchResetPassword extends BatchAction
  7. {
  8. protected $title = '修改密码';
  9. public function render()
  10. {
  11. // 实例化表单类
  12. $form = ResetPasswordForm::make();
  13. return Modal::make()
  14. ->lg()
  15. ->title($this->title)
  16. ->body($form)
  17. // 因为此处使用了表单异步加载功能,所以一定要用 onLoad 方法
  18. // 如果是非异步方式加载表单,则需要改成 onShow 方法
  19. ->onLoad($this->getModalScript())
  20. ->button($this->title);
  21. }
  22. protected function getModalScript()
  23. {
  24. // 弹窗显示后往隐藏的id表单中写入批量选中的行ID
  25. return <<<JS
  26. // 获取选中的ID数组
  27. var key = {$this->getSelectedKeysScript()}
  28. $('#reset-password-id').val(key);
  29. JS;
  30. }
  31. }

使用

  1. use App\Admin\Actions\Grid\BatchResetPassword;
  2. $grid->batchActions([new BatchResetPassword()]);