Laravel 5.2 - 验证错误(如何将页面 return 定位到滚动中的确切位置)
Laravel 5.2 - Validation Errors (How do I get the page to return to the exact point in the scroll)
我已经成功地为我的 laravel 5.2 应用程序设置了验证错误。但我的表格在页面底部。当触发验证错误时,页面会重新加载错误,但它会转到页面顶部。有什么方法可以让页面刷新并停留在该用户拥有它的滚动点上?或者在表单所在的位置 (/#calltoaction)。谢谢!
希望你能试试<form action={{route('url','#calltoaction')}} method="post">
所以这会让你喜欢
http://url?#calltoaction
这不是 Laravel 问题,而是 html 问题。
<div id="calltoaction">
// Your content here
</div>
<form action="/foo#calltoaction" method="post">
// Your form here
<input type="submit" value="Send">
</form>
通过单击按钮,有一个 POST 请求到 foo
,您将跳转到 ID 为 calltoaction
的 div。
当然,您可以使用 Laravel 中的路由助手而不是硬编码 url。
您也可以手动进行验证和重定向,而不是使用 $this->validate()
:
use Illuminate\Support\Facades\Validator;
...
$v = Validator::make($request->all(), [
'comment' => 'required',
'username' => 'required',
]);
if ($v->fails()) {
return redirect(url()->previous() . "#anchor")->withErrors($v->errors());
}
我已经成功地为我的 laravel 5.2 应用程序设置了验证错误。但我的表格在页面底部。当触发验证错误时,页面会重新加载错误,但它会转到页面顶部。有什么方法可以让页面刷新并停留在该用户拥有它的滚动点上?或者在表单所在的位置 (/#calltoaction)。谢谢!
希望你能试试<form action={{route('url','#calltoaction')}} method="post">
所以这会让你喜欢
http://url?#calltoaction
这不是 Laravel 问题,而是 html 问题。
<div id="calltoaction">
// Your content here
</div>
<form action="/foo#calltoaction" method="post">
// Your form here
<input type="submit" value="Send">
</form>
通过单击按钮,有一个 POST 请求到 foo
,您将跳转到 ID 为 calltoaction
的 div。
当然,您可以使用 Laravel 中的路由助手而不是硬编码 url。
您也可以手动进行验证和重定向,而不是使用 $this->validate()
:
use Illuminate\Support\Facades\Validator;
...
$v = Validator::make($request->all(), [
'comment' => 'required',
'username' => 'required',
]);
if ($v->fails()) {
return redirect(url()->previous() . "#anchor")->withErrors($v->errors());
}