路由器
Route::get('/contact',['uses'=>'Admin\ContactController@show','as'=>'contact']);
Route::post('/contact',['uses'=>'Admin\ContactController@store']);
控制器
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\ContactRequest;
use App\Http\Controllers\Controller;
use Validator;
class ContactController extends Controller
{
public function store(Request $request,$id=FALSE) {
if($request->isMethod('post')) {
$messages = [
'name.required' => 'ПОЛЕ :attribute обязательно к заполнению!!!!',
'email.max' => 'Максимально допустимое количество символов - :max',
];
$validator = Validator::make($request->all(),[
'name'=>'required',
/*'email'=>'required'*/
],$messages);
$validator->sometimes(['email','site'],'required',function($input) {
/*dump($input);
exit();*/
return strlen($input->name) >= 10;
});
$validator->after(function($validator) {
$validator->errors()->add('name','ДОполнительное сообщение');
});
if($validator->fails()) {
$messages = $validator->errors();
//dump ($messages->first());
dump($validator->failed());
exit();
return redirect()->route('contact')->withErrors($validator)->withInput();
}
}
return view('default.contact',['title'=>'Contacts']);
}
public function show() {
return view('default.contact',['title'=>'Contacts']);
}
}
模板
extends('default.layouts.layout')
@section('content')
<div class="col-md-9">
<div class="">
<h2>Contact us!</h2>
</div>
<p>
This is a template for a simple marketing or informational website. It includes a large callout called a jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique.
</p>
{{ count($errors)}}
@if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error}}</li>
@endforeach
</ul>
</div>
@endif
<form method="post" action="{{ route('contact') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}" placeholder="Jane Doe">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" value="{{ old('email') }}" name="email" placeholder="Email">
</div>
<div class="form-group">
<label for="site">Site</label>
<input type="text" class="form-control" id="site" value="{{ old('site') }}" name="site" placeholder="Site">
</div>
<div class="form-group">
<label for="text">Text</label>
<textarea class="form-control" id="text" name="text" rows="3">{{ old('text') }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
@endsection
问题是我没有显示验证错误和消息“进一步消息”。虽然验证成功。问题是。我不想显示 $messages 我试图在它上面行走foreach
,但 laravel 说它找到了 $messages。虽然控制器 $messages 显示。该模式还显示我的错误为 0,但在控制器中我显示了验证错误。有什么问题?