php - Laravel 5.7 验证错误未显示
问题描述
我在下面有以下功能,可以在两个选项中正常登录并重定向,但是我没有收到任何错误!- 我怎样才能覆盖错误消息?
刀:
<div>
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username'), array('class' => 'form-control', 'required' => 'required')) }}
<div class="invalid-feedback">{{ $errors->first('username') }}</div>
功能:
public function processLogin() {
// Lets create some simple validation rules for login
$rules = array(
'username' => 'required',
'password' => 'required',
);
// Lets check the guest filled in all the correct details :)
$validator = Validator::make(Input::all(), $rules);
// Lets redirect back to login page if they have not
if ($validator->fails()) {
return Redirect::to('/')
->withErrors($validator)
->withInput(Input::except('password')); //Sends back only room name
} else {
// We will create an array of login information :)
$loginData = array(
'username' => Input::get('username'),
'password' => Input::get('password'),
);
// Lets Login
if (Auth::attempt($loginData)) {
// If they logged in correct lets give them this :)
return Redirect::away('https://google.co.nz/');
} else {
// If not they can have this
return Redirect::to('/');
}
}
解决方案
好的,所以您正在尝试更改“默认”错误消息,而不是创建新规则。我看到上面的一些人一直在回答,但这是我的 2 美分,非常干净和简单。
public function store(Request $request)
{
$messages = [
'username.required' => 'You must have a username!',
'password.required' => 'Please add a password...'
];
$request->validate([
'username' => 'required',
'password' => 'required'
], $messages);
// And here is the code that should be executed if the validation is valid
return 'Everything seems to work!';
}
推荐阅读
- docker - 如何编写 dockerfile 以从我的 github 中正确提取代码
- groovy - 如何从 groovy 中的 json 对象中获取值
- c++ - g++ 中的 system() 函数
- javascript - axios 出错:TypeError:无法读取未定义的属性“状态”
- java - 找不到这个 spring orm 项目的问题。我正在尝试使用 Postgres Sql
- excel - 将单元格的垂直文本拆分为多列
- selenium - 创建屏幕截图 - Allure、JUnit5、Selenium
- linux - 每出现一次分隔符就拆分一次
- r - R,编辑电影文件或 avi 文件?
- java - 我的 AVL 树删除方法有什么问题?