Ad

FormRequest Not Generating Error Messages

- 1 answer

When the validation fails i get nothing at the $errors variable, it used to work, but now it stopped and i have no idea why.

I've tried several things, change the fields that are failing, dd'ing the request inside the different methods at the request, i've commented the different methods at the request and nothing. I've excluded the method from the middlewares I'm using (View composer middlewares) and nothing. any idea what si going on?

An important note is that the create view it's the same for edition. When it "creates" a post, hits the store method which uses the parent request PostRequest and it all works perfectly, the $errors variable gets populated.

Some code.

My posts route file:

Route::resource('posts', 'PostController');

RouteServiceProvider method where i clearly include the web middleware:

protected function mapPostsRoutes()
{
   Route::middleware('web')
        ->namespace($this->namespace)
        ->group(base_path('routes/posts.php'));
}

Where i use my custom form request at my update method, PostController:

 /**
* @param PostUpdateRequest $request
* @param Post $post
* @return Factory|RedirectResponse|View
*/
public function update(PostUpdateRequest $request, Post $post)
{
// and so on 

My child PostUpdateRequest form request for updating purposes:

<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rule;

class PostUpdateRequest extends PostRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return array_merge(
            parent::rules(), [
               'slug' => [
                   'required', 'string',
                   Rule::unique('posts', 'slug')->ignore($this->slug, 'slug')
               ],
        ]);
    }
}

Its father PostRequest

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;

class PostRequest extends FormRequest
{
    protected function prepareForValidation()
    {
        $this->merge([
            'slug' => Str::slug($this->title),
        ]);
    }

    /**
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|string',
            'content' => 'string|required|',
            'status' => 'required|string',
            'attachmentvalue' => 'string',
            'slug' => [
                'required',
                'string',
                'min:2',
                Rule::unique('posts', 'slug')
            ],
        ];
    }

    /**
     * @return array
     */
    public function messages()
    {
        return array_merge(
            parent::messages(),
            [
                'title.required' => __('El título es obligatorio'),
                'content.required' => __('El artículo debe poseer un contenido'),
                'slug.string' => __('Ya hay un artículo con ese título'),
                'attachmentvalue.string' => __('El artículo debe contener una imagen estacada'),
            ]
        );
    }

    /**
     * @return array
     */
    public function attributes()
    {
        return [
            'title' => __('Título'),
            'content' => __('Contenido'),
            'attachmentvalue' => __('Adjunto')
        ];
    }
}
Ad

Answer

I found the problem, some versions of laravel 6 have the SESSION_DRIVER set to cookie instead of file at the config/session.php. Changing that to file solved the issue.

Ad
source: stackoverflow.com
Ad