验证失败时停止表单刷新 Laravel
Stop form refreshing when validation fails Laravel
我有以下代码:
模态:
<form class="form-signin" role="form" method="POST" action="{{ url('/login') }}">
<div class="modal fade" id="signinModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
@include('application.errors')
...
<button type="submit" class="btn btn-hp-modal underline btn-signup-modal">Log in</button>
</div>
</form>
控制器:
public function login( Request $request ) {
...
// If the login attempt was unsuccessful
return $this->sendFailedLoginResponse($request);
}
protected function sendFailedLoginResponse(Request $request)
{
throw ValidationException::withMessages([
$this->username() => [trans('auth.failed')],
]);
}
application.errors:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
我遇到的问题是,在我点击 Log in
后,无论验证成功与否,它都会刷新页面。如果我在刷新页面后打开模式,则会出现该消息,但我希望模式保留,以防它实际进入 sendFailedLoginResponse
。
如有任何帮助,我们将不胜感激!
如果您想阻止页面在提交时刷新,您需要使用 JavaScript 进行 client-side 验证检查。
要阻止表单提交,您需要在表单提交处理程序中使用 event.preventDefault()
或 return false
。
您似乎是在此处向您的服务器提交,这意味着您无法为所欲为。如果要实现 client-side 验证并防止失败时重新加载页面,则必须使用 JavaScript.
这是一个伪代码示例:
$('#form').submit(function (e) {
if ($('#username').value() === '') {
// Show some validation message here...
// Prevent default and return.
return e.preventDefault();
}
// Submit to API via AJAX call.
});
我找到了另一种方法:
在验证 return 中推送一个 GET
请求来获取一些以前的数据,它不会修复刷新,但至少你可以在用户重定向时恢复数据:
// if the validator fails, redirect back to the form
if ($validator->fails()) {
//let's send some data back (Orlando,Florida):
return Redirect::to('auth/register?city=Orlando&State=Florida')
->withErrors($validator)
->withInput(Input::except('password', 'password_confirmation'));
}else{
//validation success
}
我有以下代码:
模态:
<form class="form-signin" role="form" method="POST" action="{{ url('/login') }}">
<div class="modal fade" id="signinModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
@include('application.errors')
...
<button type="submit" class="btn btn-hp-modal underline btn-signup-modal">Log in</button>
</div>
</form>
控制器:
public function login( Request $request ) {
...
// If the login attempt was unsuccessful
return $this->sendFailedLoginResponse($request);
}
protected function sendFailedLoginResponse(Request $request)
{
throw ValidationException::withMessages([
$this->username() => [trans('auth.failed')],
]);
}
application.errors:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
我遇到的问题是,在我点击 Log in
后,无论验证成功与否,它都会刷新页面。如果我在刷新页面后打开模式,则会出现该消息,但我希望模式保留,以防它实际进入 sendFailedLoginResponse
。
如有任何帮助,我们将不胜感激!
如果您想阻止页面在提交时刷新,您需要使用 JavaScript 进行 client-side 验证检查。
要阻止表单提交,您需要在表单提交处理程序中使用 event.preventDefault()
或 return false
。
您似乎是在此处向您的服务器提交,这意味着您无法为所欲为。如果要实现 client-side 验证并防止失败时重新加载页面,则必须使用 JavaScript.
这是一个伪代码示例:
$('#form').submit(function (e) {
if ($('#username').value() === '') {
// Show some validation message here...
// Prevent default and return.
return e.preventDefault();
}
// Submit to API via AJAX call.
});
我找到了另一种方法:
在验证 return 中推送一个 GET
请求来获取一些以前的数据,它不会修复刷新,但至少你可以在用户重定向时恢复数据:
// if the validator fails, redirect back to the form
if ($validator->fails()) {
//let's send some data back (Orlando,Florida):
return Redirect::to('auth/register?city=Orlando&State=Florida')
->withErrors($validator)
->withInput(Input::except('password', 'password_confirmation'));
}else{
//validation success
}