我有关于错误消息输出的问题。错误消息输出始终在之前的视图中,而不在当前视图中。
这是我的观点,我从输入用户名开始。
路线...
Route::get('invitation', 'UsernameController@search')->name('search.username');
UsernameController函数...
public function search()
{
return view('invitation.username');
}
查看...
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Eingabe Username der zu einer Gruppe eingeladen werden soll.</div>
<div class="panel-body">
@if(@count(errors > 0))
@foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
@endforeach
@endif
{!! Form::open(array('route'=>'result.username')) !!}
<div class="form-group">
{{Form::label('username', 'Username')}}
{{Form::text('username', '', ['class' => 'form-control', 'placeholder' => 'Enter Username'])}}
{{ csrf_field() }}
</div>
<div>
{{Form::submit('Submit',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
在这里,我输入用户名,并在按下提交按钮后将打开以下视图。 路线...
Route::post('invitation', 'UsernameController@result')->name('result.username');
UsernameController函数...
public function result(Request $request)
{
$request->validate([
'username' => [
'required', 'alpha_num', new ExistingUser, new UserNotAdmin
]
]);
$username = $request->username;
$user = User::where('name', $username)->select('id')->first();
$user = User::where('id', $user->id)->select('id', 'name')->first();
$groups = Group::where('admin', Auth::id())->select('id','name')->get();
return view('invitation.group')->with('groups', $groups)->with('user', $user);
}
查看...
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">In welche Gruppe wollen sie den User {{$user->name}} einladen?</div>
<div class="panel-body">
@if(@count(errors > 0))
@foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
@endforeach
@endif
{!! Form::open(array('route'=>'store.invitation')) !!}
<div class="form-group">
<input type="hidden" name="iduser" value="{{ $user->id }} "/>
<select class="form-control" name="idgroup">
@foreach ($groups as $group)
<option value="{{ $group->id }}">{{ $group->name }}</option>
@endforeach
</select>
{{ csrf_field() }}
</div>
<div>
{{Form::submit('User einladen',['class' => 'btn btn-primary'])}}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
当我按下提交,并且路径store.invitation出现错误/验证消息时,它将不会显示在第二个视图(invitation.group)中,而是会显示在第一个视图(invitation.username)中。
此处是检查是否必须显示错误/验证消息的功能。
路线...
Route::post('invitation/store', 'InvitationController@store')->name('store.invitation');
InvitationController函数...
public function store(Request $request)
{
$request->validate([
'idgroup' => [
'required', 'numeric', new AdminGroupRequest, new MaxInvitationAdmin
],
'iduser' => [
'required', 'numeric', new AlreadyInvitation, new AlreadyPartOfGroup, new MaxInvitationUser
]
]);
$iduser = $request->iduser;
$idgroup = $request->idgroup;
$i = new Invitation;
$i->idgroup = $idgroup;
$i->iduser = $iduser;
$i->active = 1;
$i->save();
$username = User::where('id', $iduser)->select('name')->first();
$groupname = Group::where('id', $idgroup)->select('name')->first();
return redirect()->action(
'UsernameController@search')->with('success', 'Sie haben den User ' .$username->name. ' in die Gruppe ' .$groupname->name. ' eingeladen.');
}
我的功能出了什么问题,或者路由是否真的不正确?