Form pop-up

Data Form Popups

The Form::dialog method can be used to quickly build a data form-based form popup, adding only a few lines of code.

{tip} The form popup is based on the following principle: the create and edit pages are used to retrieve the built form HTML characters, and then the HTML characters are rendered using a popup plugin. If a new js script needs to be loaded in the meantime, it will wait until the script is loaded and then execute the form initialization js code.

Simple example

opens form pop-up

  1. <?php
  2. use App\Http\Controllers\Controller;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Layout\Content;
  5. class ModalFormController extends Controller
  6. {
  7. public function index(Content $content)
  8. {
  9. return $content
  10. ->header('Modal Form')
  11. ->body($this->build());
  12. }
  13. protected function build()
  14. {
  15. Form::dialog('Add new roles.')
  16. ->click('.create-form') // Binding click button
  17. ->url('auth/roles/create') // Form page link, this parameter is replaced by the "data-url" attribute of the button.
  18. ->width('700px') // Specify the width of the popup window, fill in the percentage, default 720px
  19. ->height('650px') // Specify the height of the popup window, can fill in the percentage, default 690px
  20. ->success('Dcat.reload()'); // Refresh page after adding success
  21. Form::dialog('Editing roles')
  22. ->click('.edit-form')
  23. ->success('Dcat.reload()'); // Refresh the page after editing the success
  24. // When you need to bind different links to the same "class" button, put the link in the button's "data-url" attribute.
  25. $editPage = admin_base_path('auth/roles/1/edit');
  26. return "
  27. <div style='padding:30px 0'>
  28. <span class='btn btn-success create-form'> New Form Popup </span> &nbsp;&nbsp;
  29. <span class='btn btn-blue edit-form' data-url='{$editPage}'> Edit Form Popup </span>
  30. </div>
  31. ";
  32. }
  33. }

Form building and saving data

  1. <?php
  2. use App\Admin\Repositories\Role;
  3. use Dcat\Admin\Controllers\HasResourceActions;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Layout\Content;
  6. use Dcat\Admin\Admin;
  7. class RoleController
  8. {
  9. use HasResourceActions;
  10. /**
  11. * Edit interface.
  12. *
  13. * @param mixed $id
  14. * @param Content $content
  15. *
  16. * @return Content
  17. */
  18. public function edit($id, Content $content)
  19. {
  20. return $content
  21. ->header(trans('admin.roles'))
  22. ->description(trans('admin.edit'))
  23. ->body($this->form()->edit($id));
  24. }
  25. /**
  26. * Create interface.
  27. *
  28. * @param Content $content
  29. *
  30. * @return Content
  31. */
  32. public function create(Content $content)
  33. {
  34. return $content
  35. ->header(trans('admin.roles'))
  36. ->description(trans('admin.create'))
  37. ->body($this->form());
  38. }
  39. /**
  40. * Make a form builder.
  41. *
  42. * @return Form
  43. */
  44. protected function form()
  45. {
  46. return Form::make(new Role(), function (Form $form) {
  47. $form->display('id', 'ID');
  48. $form->text('slug', trans('admin.slug'))->required()->prepareForSave(function ($value) {
  49. return $value;
  50. });
  51. $form->text('name', trans('admin.name'))->required();
  52. $form->tree('permissions')
  53. ->nodes(function () {
  54. $permissionModel = config('admin.database.permissions_model');
  55. $permissionModel = new $permissionModel;
  56. return $permissionModel->allNodes();
  57. })
  58. ->customFormat(function ($v) {
  59. if (!$v) return [];
  60. return array_column($v, 'id');
  61. });
  62. $form->display('created_at', trans('admin.created_at'));
  63. $form->display('updated_at', trans('admin.updated_at'));
  64. });
  65. }
  66. }

result

Form pop-up - 图1

Functional interface

The form popup must be tied to a clickable page element that is clicked to bring up the popup.

set popupTITLE

  1. $modal = Form::dialog('TITLE');

bind click button

You can bind a clicked button via the ModalForm::click method, which will bring up a popup when the button is clicked.

  1. Form::dialog('TITLE')
  2. ->click('#click-button');

Set URL

If you are creating a form, you can get the url of the form template by setting it as follows

  1. Form::dialog('新增角色')
  2. ->click('.create-form')
  3. ->url('auth/roles/create');

If it’s an edit type form, multiple URLs are required because the content of the form that pops up with each button clicked is different, so each button has a different link.

At this time, a link set by the ModalForm::url method is no longer sufficient, so you need to save the url on the data-url property of the clicked button:

  1. Form::dialog('编辑角色')
  2. ->click('.edit-form')
  3. ->success('Dcat.reload()'); // Refresh the page after editing the success
  4. // When you need to bind different links to the same "class" button, put the link in the button's "data-url" attribute.
  5. $editPage1 = admin_base_path('auth/roles/1/edit');
  6. $editPage2 = admin_base_path('auth/roles/2/edit');
  7. return "
  8. <div style='padding:30px 0'>
  9. <span class='btn btn-blue edit-form' data-url='{$editPage1}'> Edit Form Popup1 </span>
  10. <span class='btn btn-blue edit-form' data-url='{$editPage2}'> Edit Form Popup 2 </span>
  11. </div>
  12. ";

Form save success callback

The success method can be used to set the js code that will be executed after the form saves the success, and there is a response variable in the scope of the js code.

  1. Form::dialog('Editing roles')
  2. ->click('.edit-form')
  3. ->success(
  4. <<<JS
  5. // Print the server response data
  6. console.log(response);
  7. // Prompt success information
  8. Dcat.success(response.message || 'Save success');
  9. // Refresh the page after saving the success
  10. Dcat.reload();
  11. JS
  12. );

Form Save Failure Callback

Through the error method, you can set the js code that will be executed after the form fails to save, and there is a response variable in the scope of this js code, through which you can get the json data returned by the server.

  1. Form::dialog('Editing roles')
  2. ->click('.edit-form')
  3. ->error(
  4. <<<JS
  5. // Print server response data
  6. console.log(response);
  7. JS
  8. );

Callback after form save

The saved method allows you to set the js code to be executed after the form is saved:

  • success A value of true means the success is saved, otherwise it fails.
  • response The json data returned by the server
  1. Form::dialog('Editing roles')
  2. ->click('.edit-form')
  3. ->saved(
  4. <<<JS
  5. // Print server response data
  6. console.log(response);
  7. if (success) {
  8. console.log('Save success');
  9. } else {
  10. console.log('Save failed');
  11. }
  12. JS
  13. );

Forced refresh

Pull new template data from the server each time you click a button.

  1. Form::dialog('Editing roles')
  2. ->click('.edit-form')
  3. ->forceRefresh();

Set Popup Width and Height

  1. Form::dialog('Editing roles')
  2. ->click('.edit-form')
  3. ->dimensions('50%', '400px');
  4. // or
  5. Form::dialog('Editing roles')
  6. ->click('.edit-form')
  7. ->width('50%')
  8. ->height('400px');

Tool form pop-up

The popup function of data form is usually more complicated to implement with a resource controller, so the system has a built-in popup function.

Form pop-up - 图2