当验证没有通过时,它返回到带有表单的页面,显示所有错误。在我的例子中,这是一个很长的页面,表单和错误在最底部。我如何让它回到那个页面,但有一个锚标记?这样用户就不必再向下滚动来查看表单和错误了。
在我的控制器中有这样的代码:
public function store(Request $request)
{
// Validate request...
$this->validate($request, [
'content' => 'required',
]);
// Store new post..
在我看来:
// Show errors if there are any
@if (isset($errors))
@foreach ($errors->all() as $error)
<em><p class="text-danger">{{ $error }}</p></em>
@endforeach
@endif
// Form goes here...
绝对不是larvel -4中自定义验证显示错误信息的副本。在这个问题中,锚标记甚至没有被提及,这是这个问题中最重要的部分。
这看起来有点奇怪,但它可以在Laravel 8.27.0
我想旧版本也可以。
创建一个规则锚点并将其设置在表单中:
<a id="yourform"></a>
<form action="url-where-you-process-the-form#yourform" method="post">
...
</form>
注意:url-where-you-process-the-form
不需要匹配当前页面的URL