Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update form operation docs #556

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions 6.x/crud-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,18 @@ The button makes one call for all entries, and only triggers one notification. I
<a name="creating-a-new-operation-with-a-form"></a>
#### Creating a New Operation With a Form

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:
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:

**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:

```bash
php artisan backpack:crud-operation Comment # will create a form for the entries in your list view, with the id in the URL

php artisan backpack:crud-operation Comment --no-id # will create a form, without the id in the URL (generators v4.0.4+)
```

**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:

**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:

```php
<?php
Expand Down Expand Up @@ -1017,7 +1025,7 @@ trait CommentOperation
{
$this->formDefaults(
operationName: 'comment',
// buttonStack: 'line', // alternatives: top, bottom
buttonStack: 'line', // alternatives: top, bottom
// buttonMeta: [
// 'icon' => 'la la-home',
// 'label' => 'Comment',
Expand All @@ -1034,7 +1042,7 @@ trait CommentOperation
* @param int $id
* @return \Illuminate\Contracts\View\View
*/
public function getCommentForm(int $id = null)
public function getCommentForm(int $id)
{
$this->crud->hasAccessOrFail('comment');

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

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

Expand Down Expand Up @@ -1103,6 +1111,10 @@ public function setupCommentOperation(): void
{
$this->crud->field('message')->type('textarea');
$this->crud->field('rating')->type('number');

// if you want to add a FormRequest to validate the fields you do it here.
// later when you handle the form submission, the request will be automatically validated
$this->crud->setValidation(CommentRequest::class); // this file is not automatically created. You have to create it yourself.
}

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

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

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

// alternatively if you set a FormRequest in the setupCommentOperation() method,
// the request will be validated here already

// and then save it to database
// $entry->comments()->create($valid);

Expand Down