Skip to content

Commit 1ce120b

Browse files
authored
Merge pull request #556 from Laravel-Backpack/update-form-docs
update form operation docs
2 parents 61c53df + 6c910fd commit 1ce120b

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

6.x/crud-operations.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -976,10 +976,18 @@ The button makes one call for all entries, and only triggers one notification. I
976976
<a name="creating-a-new-operation-with-a-form"></a>
977977
#### Creating a New Operation With a Form
978978

979-
Say we want to create a ```Comment``` operation. Click the Comment button on an entry, and it brings up a form with a textarea. Submit the form and you're back to the list view. What we need to do is:
979+
Say we want to create a ```Comment``` operation. Click the Comment button on an entry, and it brings up a form with a textarea. Submit the form and you're back to the list view. Let's get started. What we need to do is:
980980

981+
**Step 0.** Install ```backpack/generators``` if you haven't yet. [https://github.com/Laravel-Backpack/Generators](https://github.com/Laravel-Backpack/Generators). We have built a set of commands to help you create a new form operation easy peasy. You can use it like this:
982+
983+
```bash
984+
php artisan backpack:crud-operation Comment # will create a form for the entries in your list view, with the id in the URL
985+
986+
php artisan backpack:crud-operation Comment --no-id # will create a form, without the id in the URL (generators v4.0.4+)
987+
```
981988

982-
**Step 1.** Generate the operation trait, by running `php artisan backpack:crud-form-operation Comment`. This will create a new trait, `CommentOperation` that should look very similar to this:
989+
990+
**Step 1.** Back to our goal, lets generate the operation trait, by running `php artisan backpack:crud-form-operation Comment`. This will create a new trait, `CommentOperation` that should look very similar to this:
983991

984992
```php
985993
<?php
@@ -1017,7 +1025,7 @@ trait CommentOperation
10171025
{
10181026
$this->formDefaults(
10191027
operationName: 'comment',
1020-
// buttonStack: 'line', // alternatives: top, bottom
1028+
buttonStack: 'line', // alternatives: top, bottom
10211029
// buttonMeta: [
10221030
// 'icon' => 'la la-home',
10231031
// 'label' => 'Comment',
@@ -1034,7 +1042,7 @@ trait CommentOperation
10341042
* @param int $id
10351043
* @return \Illuminate\Contracts\View\View
10361044
*/
1037-
public function getCommentForm(int $id = null)
1045+
public function getCommentForm(int $id)
10381046
{
10391047
$this->crud->hasAccessOrFail('comment');
10401048

@@ -1051,7 +1059,7 @@ trait CommentOperation
10511059
{
10521060
$this->crud->hasAccessOrFail('comment');
10531061

1054-
return $this->formAction($id, function ($inputs, $entry) {
1062+
return $this->formAction(id: $id, formLogic: function ($inputs, $entry) {
10551063
// You logic goes here...
10561064
// dd('got to ' . __METHOD__, $inputs, $entry);
10571065

@@ -1103,6 +1111,10 @@ public function setupCommentOperation(): void
11031111
{
11041112
$this->crud->field('message')->type('textarea');
11051113
$this->crud->field('rating')->type('number');
1114+
1115+
// if you want to add a FormRequest to validate the fields you do it here.
1116+
// later when you handle the form submission, the request will be automatically validated
1117+
$this->crud->setValidation(CommentRequest::class); // this file is not automatically created. You have to create it yourself.
11061118
}
11071119

11081120
```
@@ -1114,12 +1126,15 @@ public function setupCommentOperation(): void
11141126
{
11151127
$this->crud->hasAccessOrFail('comment');
11161128

1117-
return $this->formAction($id, function ($inputs, $entry) {
1129+
return $this->formAction(id: $id, formLogic: function ($inputs, $entry) {
11181130
// You logic goes here...
11191131

11201132
// You can validate the inputs using the Laravel Validator, eg:
11211133
// $valid = Validator::make($inputs, ['message' => 'required'])->validated();
11221134

1135+
// alternatively if you set a FormRequest in the setupCommentOperation() method,
1136+
// the request will be validated here already
1137+
11231138
// and then save it to database
11241139
// $entry->comments()->create($valid);
11251140

0 commit comments

Comments
 (0)