|
13 | 13 | - [Search](#search)
|
14 | 14 | - [Multi-search](#multisearch)
|
15 | 15 | - [Pause](#pause)
|
| 16 | +- [Forms](#forms) |
16 | 17 | - [Informational Messages](#informational-messages)
|
17 | 18 | - [Tables](#tables)
|
18 | 19 | - [Spin](#spin)
|
@@ -720,6 +721,61 @@ use function Laravel\Prompts\pause;
|
720 | 721 | pause('Press ENTER to continue.');
|
721 | 722 | ```
|
722 | 723 |
|
| 724 | +<a name="forms"></a> |
| 725 | +## Forms |
| 726 | + |
| 727 | +Often, you will have multiple prompts that will be displayed in sequence to collect information before performing additional actions. You may use the `form` function to create a grouped set of prompts for the user to complete: |
| 728 | + |
| 729 | +```php |
| 730 | +use function Laravel\Prompts\form; |
| 731 | + |
| 732 | +$responses = form() |
| 733 | + ->text(label: 'What is your name?', required: true) |
| 734 | + ->password('What is your password?', validate: ['password' => 'min:8']) |
| 735 | + ->confirm('Do you accept the terms?') |
| 736 | + ->submit(); |
| 737 | +``` |
| 738 | + |
| 739 | +The `submit` method will return a numerically indexed array containing all of the responses from the form's prompts. However, you may provide a name for each prompt via the `name` argument. When a name is provided, the named prompt's response may be accessed via that name: |
| 740 | + |
| 741 | +```php |
| 742 | +use App\Models\User; |
| 743 | +use function Laravel\Prompts\form; |
| 744 | + |
| 745 | +$responses = form() |
| 746 | + ->text(label: 'What is your name?', required: true, name: 'name') |
| 747 | + ->password( |
| 748 | + 'What is your password?', |
| 749 | + validate: ['password' => 'min:8'], |
| 750 | + name: 'password', |
| 751 | + ) |
| 752 | + ->confirm('Do you accept the terms?') |
| 753 | + ->submit(); |
| 754 | + |
| 755 | +User::create([ |
| 756 | + 'name' => $responses['name'], |
| 757 | + 'password' => $responses['password'] |
| 758 | +]); |
| 759 | +``` |
| 760 | + |
| 761 | +The primary benefit of using the `form` function is the ability for the user to return to previous prompts in the form using either `CTRL + U` or `CMD + BACKSPACE`. This allows the user to fix mistakes or alter selections without needing to cancel and restart the entire form. |
| 762 | + |
| 763 | +If you need more granular control over a prompt in a form, you may invoke the `add` method instead of calling one of the prompt functions directly. The `add` method is passed all previous responses provided by the user: |
| 764 | + |
| 765 | +```php |
| 766 | +use function Laravel\Prompts\form; |
| 767 | +use function Laravel\Prompts\outro; |
| 768 | + |
| 769 | +$responses = form() |
| 770 | + ->text(label: 'What is your name?', required: true, name: 'name') |
| 771 | + ->add(function ($responses) { |
| 772 | + return text("How old are you, {$responses['name']}?"); |
| 773 | + }, name: 'age') |
| 774 | + ->submit(); |
| 775 | + |
| 776 | +outro("Your name is {$responses['name']} and you are {$responses['age']} years old."); |
| 777 | +``` |
| 778 | + |
723 | 779 | <a name="informational-messages"></a>
|
724 | 780 | ## Informational Messages
|
725 | 781 |
|
|
0 commit comments