我在互联网上搜索过它,但似乎有所不同。我正在使用laravel5.1
并实施google recaptcha
。方案是,如果表单已提交并返回验证错误,则重新连接会一次又一次地重置,我想要的是不再重置它,只是保持有效,因为它会让用户一次又一次地进行验证。你对此有什么想法吗?
更新: 代码
public function postRegister(Request $request){
// Validation
$this->validate($request, [
'username' => 'required|unique:users|max:20|min:3',
'password' => 'required|min:6',
'retype_password' => 'required|same:password',
'email' => 'required|unique:users|email|max:255',
'g-recaptcha-response' => 'required|recaptcha'
]);
// Database save part here...
return redirect()->route('register')->with('info', 'Success!');
}
答案 0 :(得分:1)
现在,我正在尝试编写代码,这有点冗长。但是你得到了要点。
首先验证您的Recaptcha字段。如果它有效,请设置会话变量以防止它再次在表单中呈现。
public function postRegister(Request $request)
{
// Prepare validation rules
$defaultRules = [
'username' => 'required|unique:users|max:20|min:3',
'password' => 'required|min:6',
'retype_password' => 'required|same:password',
'email' => 'required|unique:users|email|max:255',
];
$recaptchaRules = [
'g-recaptcha-response' => 'required|recaptcha',
];
// Set session if recaptcha is valid
if (Validator::make($request->all(), $recaptchaRules)->passes()) {
session(['recaptcha' => true]);
}
// Add recaptcha rules to default rules if failed to get single message bag with all errors
else {
$defaultRules = array_merge($defaultRules, $recaptchaRules);
}
// Validation
$this->validate($request, $defaultRules);
// Database save part here...
// Reset recaptcha validity so that the recaptcha is displayed on the next submission
session(['recaptcha' => false]);
return redirect()->route('register')->with('info', 'Success!');
}
如果尚未验证,则仅输出Recaptcha字段。
@unless (session('recaptcha'))
{{ Recaptcha::render() }}
@endunless