我下面有以下功能,可以正常登录和重定向两个选项,但是我没有收到任何错误! -如何也可以覆盖错误消息?
刀片:
<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('/');
}
}
答案 0 :(得分:0)
为了替换错误消息,请为自定义消息创建一个数组:
$customMessages = [
'username.required' => 'Username cannot be empty.',
'password.required' => 'Password cannot be empty.',
];
$validator = Validator::make($request->all(),$rules,$customMessages);
您可以查看文档here。
答案 1 :(得分:0)
验证规则:
$rules = [
'username' => 'required',
'password' => 'required',
];
如果验证失败,则自定义消息
$messages = ['username' => 'error with username.',
'password'=>'error with password.'];
使用规则验证输入,添加自定义消息
$validator = Validator::make(Input::all(), $rules, $messages);
答案 2 :(得分:0)
嘿哥们,您没有传递任何请求参数,因为它不起作用
public function processLogin(Request $request) {
// 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('/');
}
}
别忘了导入请求类
use Illuminate\Http\Request;
删除缓存以在终端上提供以下命令
php artisan config:cahce
php artisan view:clear
php artisan route:clear
答案 3 :(得分:0)
Okey,因此您尝试更改“默认”错误消息,而不创建新规则。我看到上面有一些答案,但这是我的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!';
}