laravel 5.4:未定义 属性:Illuminate\Validation\Validator::$错误
laravel 5.4: Undefined property: Illuminate\Validation\Validator::$errors
我这里有一个电子邮件验证代码,用户在其中注册,注册后他会接受注册确认电子邮件。我正在使用 laravel make auth 进行修改以包括将令牌发送到有效电子邮件。注册成功后应该会出现一条确认消息,但实际上并没有。
代码如下:
注册控制器
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Mail;
use Illuminate\HTTP\Request;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
protected function register(Request $request){
$input = $request->all();
$validator = $this->validator($input);
if ($validator->passes()){
$data = $this->create($input)->toArray();
$data['token'] = str_random(25);
$user= User::find($data['id']);
$user->token=$data['token'];
$user->save();
Mail::send('mail.confirmation', $data, function($message) use($data){
$message->to($data['email']);
$message->subject('Registration Confirmation');
});
return redirect(route('login'))->with('status','confirmation email has been sent');
}
return redirect(route('login'))->with('status', $validator->errors);
}
public function confirmation($token)
{
$user = User::where('token', $token)->first();
if(!is_null($user)){
$user->confirmed=1;
$user->token='';
$user->save();
return redirect(route('login'))->with('status','complete actibeyt');
}
return redirect(route('login'))->with('status','something is wrong');
}
}
第一个用这个替换你的return函数:-
return redirect()->route('login')->with('status','confirmation email has been sent');
Validator
上没有名为 errors
的 属性,但是,有一个方法。
简单的改变:
return redirect(route('login'))->with('status', $validator->errors());
看起来你已经解决了眼前的问题,但我创建了一个 Web 服务来解决这个特殊的痛点,根本没有服务器端代码:clicktoverify.net。它只需要一个 JS 包含和几行代码:
<form method="POST" action="whatever" id="my-form">
<input type="email" name="email" />
<input type="submit" name="sign up!" />
</form>
<script type="text/javascript" src="https://cdn.clicktoverify.net/ctv.js"></script>
<script type="text/javascript">
form = document.getElementById("my-form");
form.addEventListener('submit', function(event) {
event.preventDefault();
alert("We've sent a verification email to this address. Please check your inbox.");
verifier = new CTV();
verifier.verify({
email: form['email'].value,
public_api_key: "71917c69-b8e8-3992-adf5-c2de3b89c17e", //you'll get your own when you create an account
success: function(xhr) {
alert("Your email address is verified!");
form.submit();
},
error: function(xhr) {
alert("Something went wrong! We couldn't verify your email address.");
}
});
});
</script>
我这里有一个电子邮件验证代码,用户在其中注册,注册后他会接受注册确认电子邮件。我正在使用 laravel make auth 进行修改以包括将令牌发送到有效电子邮件。注册成功后应该会出现一条确认消息,但实际上并没有。
代码如下:
注册控制器
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Mail;
use Illuminate\HTTP\Request;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
protected function register(Request $request){
$input = $request->all();
$validator = $this->validator($input);
if ($validator->passes()){
$data = $this->create($input)->toArray();
$data['token'] = str_random(25);
$user= User::find($data['id']);
$user->token=$data['token'];
$user->save();
Mail::send('mail.confirmation', $data, function($message) use($data){
$message->to($data['email']);
$message->subject('Registration Confirmation');
});
return redirect(route('login'))->with('status','confirmation email has been sent');
}
return redirect(route('login'))->with('status', $validator->errors);
}
public function confirmation($token)
{
$user = User::where('token', $token)->first();
if(!is_null($user)){
$user->confirmed=1;
$user->token='';
$user->save();
return redirect(route('login'))->with('status','complete actibeyt');
}
return redirect(route('login'))->with('status','something is wrong');
}
}
第一个用这个替换你的return函数:-
return redirect()->route('login')->with('status','confirmation email has been sent');
Validator
上没有名为 errors
的 属性,但是,有一个方法。
简单的改变:
return redirect(route('login'))->with('status', $validator->errors());
看起来你已经解决了眼前的问题,但我创建了一个 Web 服务来解决这个特殊的痛点,根本没有服务器端代码:clicktoverify.net。它只需要一个 JS 包含和几行代码:
<form method="POST" action="whatever" id="my-form">
<input type="email" name="email" />
<input type="submit" name="sign up!" />
</form>
<script type="text/javascript" src="https://cdn.clicktoverify.net/ctv.js"></script>
<script type="text/javascript">
form = document.getElementById("my-form");
form.addEventListener('submit', function(event) {
event.preventDefault();
alert("We've sent a verification email to this address. Please check your inbox.");
verifier = new CTV();
verifier.verify({
email: form['email'].value,
public_api_key: "71917c69-b8e8-3992-adf5-c2de3b89c17e", //you'll get your own when you create an account
success: function(xhr) {
alert("Your email address is verified!");
form.submit();
},
error: function(xhr) {
alert("Something went wrong! We couldn't verify your email address.");
}
});
});
</script>