W
W
webfln2016-12-18 13:32:09
Laravel
webfln, 2016-12-18 13:32:09

Why doesn't Laravel 5.3 want to show validation errors?

I am learning Laravel. Now I'm analyzing the topic with data validation and I can't display errors.
I created a request class:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ContactRequest extends FormRequest
{
    /**
     * 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' => 'Er, you forgot your name!',
       ];
    }
}

I also created a controller for the page.
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\ContactRequest;
use App\Http\Controllers\Controller;

use Validator;


class ContactController extends Controller
{
   
    public function store(ContactRequest $request) {
        
        return view('default.contact',['title'=>'Contacts']);
    }
    

    public function show()
    {
        return view('default.contact',['title'=>'Contacts']);
    }

}

I also added the necessary providers to the $middleware property
protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    ];

Created 2 routers:
Route::get('/contact',['uses'=>'Admin\[email protected]','as'=>'contact']);
Route::post('/contact',['uses'=>'Admin\[email protected]']);

(I also tried to combine them into the middleware => web group (it didn’t help either))
The form itself looks like this:
@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

    <div class="form-area">  
        <form method="post" action="{{ route('contact') }}">
        {{ csrf_field() }}

    				<div class="form-group">
            <input type="text" class="form-control" value="{{ old('name') }}" id="name" name="name" placeholder="Name">
          </div>

          <div class="form-group">
            <input type="text" class="form-control" value="{{ old('email') }}" id="email" name="email" placeholder="Email">
          </div>

          <div class="form-group">
            <input type="text" class="form-control" value="{{ old('site') }}" id="site" name="site" placeholder="site">
          </div>

                    <div class="form-group">
                    <textarea class="form-control" type="textarea" name="text" id="message" placeholder="Message" maxlength="140" rows="7">{{ old('text') }}</textarea>
                        <span class="help-block"><p id="characterLeft" class="help-block ">You have reached the limit</p></span>                    
                    </div>
            
       			 <input type="submit" id="submit" name="submit" class="btn btn-primary">
        </form>
    </div>

But in the end, when submitting the form, no error messages are displayed. Please tell me where to dig? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
webfln, 2016-12-18
@webfln

In general, thanks to Arik for help. The problem was to create a session from scratch. That is why the errors were not displayed. I solved the problem by demolishing the framework and installing from scratch.

S
Sergey Semenko, 2016-12-18
@abler98

https://github.com/laravel/framework/blob/5.3/src/...
https://github.com/laravel/framework/blob/5.3/src/...

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question