1

I have a form request that validates 'name' attribute as required. When I submit my form it redirects back to the create page without saving but I don't receive any error messages.

When I do a var_dump on $errors, it is empty. I'd appreciate help figuring out what is happening here...

View

@if($errors->any())
  <ul class="alert alert-danger">
    @foreach($errors->all() as $error)
      <li>{{ $error }}</li>
    @endforeach
  </ul>
@endif
{{var_dump($errors)}}

Controller:

<?php

namespace CRM\Http\Controllers\Settings;

use CRM\Http\Requests;
use CRM\Http\Controllers\Controller;

use CRM\Category;
use CRM\Http\Requests\CategoryFormRequest;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Session;

class CategoriesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return void
     */
    public function index()
    {
        $categories = Category::paginate(15);

        return view('settings.categories.index', compact('categories'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return void
     */
    public function create()
    {
        return view('settings.categories.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param CategoryFormRequest $request
     */
    public function store(CategoryFormRequest $request)
    {

        Category::create($request->all());

        Session::flash('flash_message', 'Category added!');

        return redirect('settings/categories');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function show($id)
    {
        $category = Category::findOrFail($id);

        return view('settings.categories.show', compact('category'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function edit($id)
    {
        $category = Category::findOrFail($id);

        return view('settings.categories.edit', compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function update($id, Request $request)
    {
        $this->validate($request, ['name' => 'required', ]);

        $category = Category::findOrFail($id);
        $category->update($request->all());

        Session::flash('flash_message', 'Category updated!');

        return redirect('settings/categories');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function destroy($id)
    {
        Category::destroy($id);

        Session::flash('flash_message', 'Category deleted!');

        return redirect('settings/categories');
    }
}

Form Request:

<?php

namespace CRM\Http\Requests;

use CRM\Http\Requests\Request;

class CategoryFormRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required'
        ];
    }

    public function messages()
    {
        return [
            'name.required' => 'Please enter a name for this category'
        ];
    }
}

Routes:

Route::group(['middleware' => ['web']], function () {
    Route::resource('settings/categories', 'Settings\\CategoriesController');
    Route::resource('settings/statuses', 'Settings\\StatusesController');
    Route::get('/', function () {
        return view('welcome');
    });
});

If i put this in the first line of store() dd($request); I get this:

Request {#40 ▼
  #json: null
  #convertedFiles: null
  #userResolver: Closure {#211 ▶}
  #routeResolver: Closure {#210 ▶}
  +attributes: ParameterBag {#42 ▶}
  +request: ParameterBag {#41 ▶}
  +query: ParameterBag {#48 ▶}
  +server: ServerBag {#45 ▶}
  +files: FileBag {#44 ▶}
  +cookies: ParameterBag {#43 ▶}
  +headers: HeaderBag {#46 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/settings/categories"
  #requestUri: "/settings/categories"
  #baseUrl: ""
  #basePath: null
  #method: "POST"
  #format: null
  #session: Store {#240 ▶}
  #locale: null
  #defaultLocale: "en"
}
showFocus
  • 701
  • 2
  • 8
  • 21

2 Answers2

0

in laravel 5.2 you need to use "web" middleware for your problem,like that

Route::group(['middleware' => ['web']], function () {
 // here you should put your routes
  });
paranoid
  • 6,799
  • 19
  • 49
  • 86
0

So having my routes inside web middleware was the cause of this issue. Apparently, in the latest version of Laravel web middleware is assumed.

I got my answer from the accepted answer on this question: Laravel 5.2 Not returning error array

Community
  • 1
  • 1
showFocus
  • 701
  • 2
  • 8
  • 21