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

[Feature Request] Validation rules and messages inside CrudResource #3

Open
tabacitu opened this issue Jun 30, 2022 · 0 comments · May be fixed by #7
Open

[Feature Request] Validation rules and messages inside CrudResource #3

tabacitu opened this issue Jun 30, 2022 · 0 comments · May be fixed by #7
Assignees
Labels
enhancement New feature or request

Comments

@tabacitu
Copy link
Collaborator

Feature Request

What I am trying to achieve

It feels a bit odd to me to define the validation rules in a different file, when everything else is defined in the CrudResource.

How I see it implemented

Starting with Backpack v5, we have three ways of adding validation rules, one way being directly on the fields. I see THAT as being the most useful if you're defining a CrudRequest. But I'd also expect to be able to define validationRules() and validationMessages() on the CrudResource... maybe... I don't know...

The end result, to me, could look something like:

class BookCrudResource extends CrudResource
{
    public function fields(): array
    {
        return [
            Text::make('Name')->size(9)->validationRules('required'), // should already work, I think
            Number::make('Year')->size(3)->required(), // alias, todo maybe
            Textarea::make('Description')->onlyOnForms(),
            Text::make('ISBN'),
        ];
    }
}

or like this:

class BookCrudResource extends CrudResource
{
    public function fields(): array
    {
        return [
            Text::make('Name')->size(9)->validationRules('required'), // should already work, I think
            Number::make('Year')->size(3)->required(), // alias, todo maybe
            Textarea::make('Description')->onlyOnForms(),
            Text::make('ISBN'),
        ];
    }

    public function validationRules(): array
    {
        // devs could test the operation here, and do stuff depending on the operation name
        // eg: if ($this->crud->operation == 'update') { change_a_rule(); }

        return [
            'isbn' => 'required|string|unique:books,isbn',
            'name' => 'required|string',
            'description' => 'nullable|string',
            'year' => 'required|integer',
        ];
    }

    // public function validationMessages(): array { return []; }
}

This should clean up the CrudController a bit, so that... ideally, you don't have to do ANYTHING inside the setupXxxOperation() method (more on that in a separate issue).

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\BookRequest;
use App\CrudResources\BookCrudResource;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

/**
 * Class BookCrudController
 * @package App\Http\Controllers\Admin
 * @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
 */
class BookCrudController extends CrudController
{
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

    private BookCrudResource $crudResource;

    /**
     * Configure the CrudPanel object. Apply settings to all operations.
     *
     * @return void
     */
    public function setup()
    {
        CRUD::setModel(\App\Models\Book::class);
        CRUD::setRoute(config('backpack.base.route_prefix') . '/book');
        CRUD::setEntityNameStrings('book', 'books');

        $this->crudResource = new BookCrudResource($this->crud);
    }

    /**
     * Define what happens when the List operation is loaded.
     *
     * @see  https://backpackforlaravel.com/docs/crud-operation-list-entries
     * @return void
     */
    protected function setupListOperation()
    {
        $this->crudResource->buildList();
    }

    /**
     * Define what happens when the Create operation is loaded.
     *
     * @see https://backpackforlaravel.com/docs/crud-operation-create
     * @return void
     */
    protected function setupCreateOperation()
    {
-        CRUD::setValidation(BookRequest::class);
-
        $this->crudResource->buildCreateForm();
    }

    /**
     * Define what happens when the Update operation is loaded.
     *
     * @see https://backpackforlaravel.com/docs/crud-operation-update
     * @return void
     */
    protected function setupUpdateOperation()
    {
-        CRUD::setValidation(BookRequest::class);
-
        $this->crudResource->buildUpdateForm();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants