Skip to content

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

@tabacitu

Description

@tabacitu

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();
    }
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions