Laravel MVC (PHP),请求错误消息
Laravel MVC (PHP), Request error messages
我正在我的系统上开发一个页面来更改用户密码,验证工作正常,但我没有收到任何错误消息。
(我没有显示 USES/imports,但没关系)
这是我的请求:
class SenhaRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'senha_antiga' => 'required|min:6|max:24',
'nova_senha' => 'required|min:6|max:24|same:nova_senha_r',
'nova_senha_r' => 'required|min:6|max:24|same:nova_senha'
];
}
public function messages() {
return [
'same' => 'A :attribute e a :other devem ser iguais.',
'required' => 'O campo :attribute é obrigatório!',
];
}
}
这是我的控制器:
class MudarSenhaController extends Controller {
public function index() {
return view('account/senha');
}
public function update(SenhaRequest $request) {
$user = Auth::user();
$user->password = bcrypt($request->input('nova_senha'));
$user->save();
return redirect()->action('Account\AccountController@index');
}
}
在我看来我有:
@if(!empty($errors->all()))
<div class="alert alert-danger">
<ul>
@foreach($errors->all as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
当某些验证失败时,会出现带有警报的 div,但我没有收到任何消息。我是 laravel 和 OO php 的新手,抱歉。
正确的代码(如@Jeemusu所说)应该是这样的:
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
(Laravel 5.4 and 5.5 documentation)
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
我正在我的系统上开发一个页面来更改用户密码,验证工作正常,但我没有收到任何错误消息。
(我没有显示 USES/imports,但没关系)
这是我的请求:
class SenhaRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'senha_antiga' => 'required|min:6|max:24',
'nova_senha' => 'required|min:6|max:24|same:nova_senha_r',
'nova_senha_r' => 'required|min:6|max:24|same:nova_senha'
];
}
public function messages() {
return [
'same' => 'A :attribute e a :other devem ser iguais.',
'required' => 'O campo :attribute é obrigatório!',
];
}
}
这是我的控制器:
class MudarSenhaController extends Controller {
public function index() {
return view('account/senha');
}
public function update(SenhaRequest $request) {
$user = Auth::user();
$user->password = bcrypt($request->input('nova_senha'));
$user->save();
return redirect()->action('Account\AccountController@index');
}
}
在我看来我有:
@if(!empty($errors->all()))
<div class="alert alert-danger">
<ul>
@foreach($errors->all as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
当某些验证失败时,会出现带有警报的 div,但我没有收到任何消息。我是 laravel 和 OO php 的新手,抱歉。
正确的代码(如@Jeemusu所说)应该是这样的:
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
(Laravel 5.4 and 5.5 documentation)
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif