Skip to content

Commit ae7f39a

Browse files
[11.x] Adds documentation for Prompts form support (laravel#9581)
* [11.x] Adds documentation for Prompts form support. * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9784273 commit ae7f39a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

prompts.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Search](#search)
1414
- [Multi-search](#multisearch)
1515
- [Pause](#pause)
16+
- [Forms](#forms)
1617
- [Informational Messages](#informational-messages)
1718
- [Tables](#tables)
1819
- [Spin](#spin)
@@ -720,6 +721,61 @@ use function Laravel\Prompts\pause;
720721
pause('Press ENTER to continue.');
721722
```
722723

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+
723779
<a name="informational-messages"></a>
724780
## Informational Messages
725781

0 commit comments

Comments
 (0)