[英]Laravel 5.7 Validation errors not showing
I have the following function below that is logging in fine and redirecting fine in both options however I am not getting any errors!! 我下面有以下功能,可以正常登录和重定向两个选项,但是我没有收到任何错误! - How can I also override the error messages?
-如何也可以覆盖错误消息?
Blade: 刀:
<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>
Function: 功能:
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('/');
}
}
in order to replace the error message, create an array for the custom messages: 为了替换错误消息,请为自定义消息创建一个数组:
$customMessages = [
'username.required' => 'Username cannot be empty.',
'password.required' => 'Password cannot be empty.',
];
$validator = Validator::make($request->all(),$rules,$customMessages);
Validation rules: 验证规则:
$rules = [
'username' => 'required',
'password' => 'required',
];
Custom messages if validation fails 验证失败时的自定义消息
$messages = ['username' => 'error with username.',
'password'=>'error with password.'];
Validate input with rules, adding in custom messages 使用规则验证输入,添加自定义消息
$validator = Validator::make(Input::all(), $rules, $messages);
buddy, you don't pass any request parameter for that it's not working 伙伴,您没有传递任何请求参数,因为它不起作用
public function processLogin(Request $request) {
// Let's create some simple validation rules for login
$rules = array(
'username' => 'required',
'password' => 'required',
);
// let's check the guest filled in all the correct details :)
$validator = Validator::make(Input::all(), $rules);
// Let's 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 incorrect let's give them this :)
return Redirect::away('https://google.co.nz/');
} else {
// If not they can have this
return Redirect::to('/');
}
}
don't forget to import request class 不要忘记导入请求类
use Illuminate\Http\Request;
remove cache to give the following command on your terminal 删除缓存以在终端上给出以下命令
php artisan config:cache
php artisan view:clear
php artisan route:clear
Okey, so you are trying to change the "default" errors messages, and not create a new rule. Okey,因此您尝试更改“默认”错误消息,而不创建新规则。 I see some above have been answering but this is my 2 cents, very clean and simple.
我看到上面有一些答案,但这是我的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!';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.