Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

Editable select custom validation message is not showing #1229

Open blessen131 opened 2 hours ago

blessen131 commented 2 hours ago

StatusRequest

public function rules() { return [ 'status' => 'in:No Show,Completed', ]; } public function attributes() { return [ 'status' =>'Status' ]; } public function messages() { return [ 'status.in' => 'Not allowed to update other status' ]; }

Controller protected function setupMinorUpdateOperation() { $this->crud->setValidation(StatusRequest::class); }

backpack: 5.x

backpack-ai-bot[bot] commented 2 hours ago

To address your issue with custom validation messages not showing for editable select in Backpack 5.x, there are a few things we need to consider:

  1. First, ensure that you have enabled grouped and inline errors in your controller. Add these lines to your setupMinorUpdateOperation() method:
protected function setupMinorUpdateOperation()
{
    $this->crud->setValidation(StatusRequest::class);
    $this->crud->enableGroupedErrors();
    $this->crud->enableInlineErrors();
}

This is crucial for displaying validation errors in Backpack forms, as mentioned in the Github issue discussion.

  1. For editable columns specifically, there was a bug fix in version 3.0.6 of the Editable Columns add-on that added support for custom validation messages. Make sure you're using at least this version of the add-on. You can check this in the Editable Columns Changelog.

  2. If you're still not seeing the custom messages, there might be an issue with how the editable column is set up. Make sure your editable select column is properly defined in your controller. It should look something like this:

CRUD::addColumn([
    'name' => 'status',
    'label' => 'Status',
    'type' => 'editable_select',
    'options' => ['No Show' => 'No Show', 'Completed' => 'Completed'],
]);
  1. If the problem persists, you might need to modify the saveMinorUpdateFormValidation() method in your controller to explicitly use the custom messages from your FormRequest. This approach was suggested in a Github issue for a similar problem:
public function saveMinorUpdateFormValidation()
{
    $input = request()->only(['id', 'attribute', 'value', 'row']);

    // Validate attribute is fillable
    if (!$this->crud->model->isFillable($input['attribute'])) {
        throw ValidationException::withMessages([
            $input['attribute'] => [__('backpack.editable-columns::minor_update.error_fillable', $input)],
        ]);
    }

    // Setting the value to its own key (for correct validation error messages)
    $input[$input['attribute']] = $input['value'];

    // Form Validation
    $formRequest = $this->crud->getFormRequest();
    if ($formRequest) {
        $formRequestObject = new $formRequest();
        $attributes = $formRequestObject->attributes();
        $messages = $formRequestObject->messages();
        $rules = $formRequestObject->rules();

        $validator = Validator::make($input, [
            $input['attribute'] => $rules[$input['attribute']] ?? '',
        ], $messages, $attributes);

        if ($validator->fails()) {
            throw new ValidationException($validator);
        }
    }
}

If after trying these steps you're still not seeing the custom validation messages, it might be a bug in the specific version of Backpack or the Editable Columns add-on you're using. In that case, you may want to report the issue on the Backpack GitHub repository or community forum.

Relevant sources:

Editable Columns|Usage Part 4 Github Issue (closed): [Editable Columns] [Bug] [Minor improvement] Empty custom messages Editable Columns|Usage Part 4 How to show error messages when you make a custom operation Update Operation|Validating fields using a rules array Github Issue (closed): [Bug] Validation errors not shown in custom operation with laravel 8 and backpack 4.1 Github Issue (closed): [Bug] forms with custom errorBags don't show error messages Editable Columns|Editable Columns Create Operation|(B) Validating fields using a rules array Github Issue (closed): [Bug] Upgrade no CRUD update and no login Github Issue (closed): Laravel backpack Customized fields are not working for Edit and Update methods