Step-by-step form

Installation

Go to https://github.com/dcat-admin/form-step to download the step-by-step form extension, then install and enable it.

{tip} To install the extension, please refer to the [Basic Use of Extensions] (extension-f.md) section of the documentation.

Simple example

  1. protected function form()
  2. {
  3. return Form::make(new Model(), function (Form $form) {
  4. $form->title('Step-by-Step Form');
  5. $form->action('step');
  6. $form->disableListButton();
  7. $form->multipleSteps()
  8. ->remember() // remember form steps, not enabled by default
  9. ->width('950px')
  10. ->add('General Information', function ($step) {
  11. $info = '<i class="fa fa-exclamation-circle"></i> The form fields support a mix of front-end and back-end validation, and front-end validation supports html form validation and custom validation.';
  12. $step->html(Alert::make($info)->info());
  13. $step->text('name', 'Fullname')->required()->maxLength(20);
  14. // h5 Form Validation
  15. $step->text('age', 'Age')
  16. ->required()
  17. ->type('number')
  18. ->attribute('max', 150)
  19. ->help('Front-end validation');
  20. $step->radio('sex', 'Sex')->options(['rather not say', 'male', 'woman'])->default(0);
  21. // Back-end validation
  22. $step->text('birthplace', 'place of ancestry')
  23. ->rules('required')
  24. ->help('Demonstrate back-end field validation');
  25. $step->url('homepage', 'personal homepage');
  26. $step->textarea('description', 'INTRODUCTION');
  27. })
  28. ->add('Hobbies', function ($step) {
  29. $step->tags('hobbies', 'preferences')
  30. ->options(['sing', 'dance', 'rap', 'play football'])
  31. ->required();
  32. $step->text('books', 'Book');
  33. $step->text('music', 'Music');
  34. // events
  35. $step->shown(function () {
  36. return <<<JS
  37. Dcat.info('hobbies');
  38. console.log('hobbies', args);
  39. JS;
  40. });
  41. })
  42. ->add('Address', function ($step) {
  43. $step->text('address', 'street address');
  44. $step->text('post_code', 'zip code');
  45. $step->tel('tel', ' telephone number');
  46. })
  47. ->done(function () use ($form) {
  48. $resource = $form->getResource(0);
  49. $data = [
  50. 'title' => 'Operating Success',
  51. 'description' => 'Congratulations on being the 10086th user!',
  52. 'createUrl' => $resource,
  53. 'backUrl' => $resource,
  54. ];
  55. return view('admin::form.done-step', $data);
  56. });
  57. });
  58. }

result

Step-by-step form - 图1

Running logic

Step-by-step form is very simple to use, and the running logic is not much different from the ordinary form, the following is a brief description of step-by-step form running logic.

{tip} Step-by-step forms do not have the concept of update.

Parameter validation

When the user clicks Next, a request will be sent to the backend to verify if the parameter is correct or not, and an error message will be displayed if the parameter does not meet the requirement.

form submission

If the parameters do not meet the requirements, it will display an error message and save the form data if the validation is passed.

{tip} The final method to save the form is Form::store.

complete page

This step cannot be ignored as the completed page will be displayed after the form is saved.

Edit Form

Step-by-step form has no editing function by default, users do not need to edit step-by-step after entering a long step form, so if you need to edit step-by-step form, you can refer to the following way

  1. protected function form()
  2. {
  3. return Form::make(new MyRepository(), function (Form $form) {
  4. // Determine if it is an edit page
  5. if ($form->isEditing()) {
  6. $form->text('age', 'Age')
  7. ->required()
  8. ->type('number')
  9. ->attribute('max', 150)
  10. ->help('Front-end validation')
  11. ...
  12. return;
  13. }
  14. $form->multipleSteps()
  15. ->remember()
  16. ->width('950px')
  17. ->add('General Information', function (Form\StepForm $step) {
  18. $info = '<i class="fa fa-exclamation-circle"></i> The form fields support a mix of front-end and back-end validation, and front-end validation supports H5 form validation and custom validation.';
  19. $step->html(Alert::make($info)->info());
  20. $step->text('name', 'Fullname')->required()->maxLength(20);
  21. // h5 Form Validation
  22. $step->text('age', 'Age')
  23. ->required()
  24. ->type('number')
  25. ->attribute('max', 150)
  26. ->help('Front-end validation');
  27. $step->radio('sex', 'Sex')->options(['rather not say', 'male', 'woman'])->default(0);
  28. ...
  29. })
  30. ->add('hobbies', function (Form\StepForm $step) {
  31. ...
  32. })
  33. ->done(function () use ($form) {
  34. ...
  35. });
  36. });
  37. }

Functional interface

Set the maximum container width

默认 1000px

{tip} This method is only for large screens and the mobile page will automatically resize.

  1. $form->multipleSteps()->width('900px');

Remembering form data

When this feature is enabled, when the user clicks the Next button and the parameters are validated, the form data will be saved in session and will not be destroyed until the entire step-by-step form is saved.

{tip} This feature is not enabled by default.

  1. // turn on
  2. $form->multipleSteps()->remember();
  3. // turn off
  4. $form->multipleSteps()->remember(false);

Set container spacing

Default value 30px 18px 30px.

  1. $form->multipleSteps()->padding('30px 18px 30px');

Listening for page exit events

Listen for all step form page leave events, multiple can be added.

  1. $form->multipleSteps()->leaving(<<<JS
  2. // Get the step index of the current page
  3. var index = args.index;
  4. Dcat.info("You are leaving the " + (index + 1) + " page");
  5. // The args variable is a js object that contains the current event object, current step option, form object and form value fields.
  6. console.log("leaving", args);
  7. // Get the current event object
  8. var evt = args.event;
  9. // Get the step form TITLEtab object
  10. var tab = args.tab;
  11. // Whether to go to the previous or next page to get the movement: "forward", "backward"
  12. var direction = args.direction;
  13. // Get the form JQ object for the current step
  14. var $form = args.form;
  15. // Get the form values for the current step page
  16. var formArray = args.formArray;
  17. // Get the form JQ object for the specified step
  18. var $firstForm = args.getForm(0);
  19. // Get form values for a specified step
  20. var firstFormArray = args.getFormArray(0);
  21. // Stop leaving the current page
  22. return false;
  23. JS
  24. )->leaving(...);

Listening for page display events

Listen to all step form page display events, multiple can be added.

  1. $form->multipleSteps()->shown(<<<JS
  2. // Get the step index of the current page
  3. var index = args.index;
  4. Dcat.info("The current display is no. " + (index + 1) + " page");
  5. // The value of the args variable is the same as the value of the "leaving" event.
  6. console.log("shown", args);
  7. JS
  8. )->shown(...);

Adding step sheets

Step forms support all form fields.

  1. use Dcat\Admin\Form;
  2. $form->multipleSteps()->add('TITLE', function (Form\StepForm $step) {
  3. $step->text('username')->rules('required');
  4. ...
  5. });

Listening for step page leaving events

Listen to the current step page leave event, with multiple listeners.

  1. use Dcat\Admin\Form;
  2. $form->multipleSteps()->add('General Information', function (Form\StepForm $step) {
  3. ...
  4. $step->leaving(<<<JS
  5. Dcat.info("You're Leaving General Information Page");
  6. // The args variable is a js object that contains the current event object, current step option, form object and form value fields.
  7. console.log("Leave General Information", args);
  8. // Get the current event object
  9. var evt = args.event;
  10. // Get the step index of the current page
  11. var index = args.index;
  12. // Get the step form TITLEtab object
  13. var tab = args.tab;
  14. // Whether to go to the previous or next page to get the movement: "forward", "backward"
  15. var direction = args.direction;
  16. // Get the form JQ object for the current step
  17. var $form = args.form;
  18. // Get the form values for the current step page
  19. var formArray = args.formArray;
  20. // Get the form JQ object for the specified step
  21. var $firstForm = args.getForm(0);
  22. // Get form values for a specified step
  23. var firstFormArray = args.getFormArray(0);
  24. // Stop leaving the current page
  25. return false;
  26. JS
  27. );
  28. // Multiple listeners
  29. $step->leaving(...);
  30. });

Listening for step page display events

Listen to the current step page to display events, and listen multiple times.

  1. use Dcat\Admin\Form;
  2. $form->multipleSteps()->add('General Information', function (Form\StepForm $step) {
  3. ...
  4. $step->shown(<<<JS
  5. Dcat.info("The current steps is General Information");
  6. // The value of the args variable is the same as the value of the "leaving" event.
  7. console.log("Show General Information", args);
  8. JS
  9. );
  10. // Multiple listeners
  11. $step->shown(...);
  12. });

Setting up the completion page

When all the steps are completed the completion page will be displayed, the system provides a default completion page, the developer can also customize the content to be displayed on the completion page by the following methods.

  1. use Dcat\Admin\Form;
  2. $form->multipleSteps()->done(function (Form\DoneStep $done) {
  3. // Get the new ID
  4. // The value returned by Repository::store
  5. $newId = $done->getNewId();
  6. // Returns what you want to display, which can make a view also a string.
  7. return view(...);
  8. });