inf*_*iac 68
您可以使用以下内容:
return Redirect::back()->withInput(Input::all());
Run Code Online (Sandbox Code Playgroud)
如果您正在使用表单请求验证,这正是Laravel将重定向您的错误和给定输入的方式.
摘录自\Illuminate\Foundation\Validation\ValidatesRequests
:
Run Code Online (Sandbox Code Playgroud)return redirect()->to($this->getRedirectUrl()) ->withInput($request->input()) ->withErrors($errors, $this->errorBag());
小智 39
例如,在字段值上写旧函数
<input type="text" name="username" value="{{ old('username') }}">
Run Code Online (Sandbox Code Playgroud)
Man*_*ash 14
在您的HTML中,您必须使用value = {{ old('') }}
.如果不使用它,您将无法获取值,因为会话将存储在其缓存中.
与名称验证一样,这将是 -
<input type="text" name="name" value="{{ old('name') }}" />
Run Code Online (Sandbox Code Playgroud)
现在,如果重定向出错,您可以在提交后获取该值.
return redirect()->back()->withInput();
Run Code Online (Sandbox Code Playgroud)
正如@infomaniac所说,你也可以Input class
直接使用,
return Redirect::back()->withInput(Input::all());
Run Code Online (Sandbox Code Playgroud)
添加:
如果您只显示特定字段,请使用$request->only()
return redirect()->back()->withInput($request->only('name'));
Run Code Online (Sandbox Code Playgroud)
希望,它可能在所有情况下都有效,谢谢.
这肯定会奏效!!!
$v = Validator::make($request->all(),[
'name' => ['Required','alpha']
]);
if($v->passes()){
print_r($request->name);
}
else{
//this will return the errors & to check put "dd($errors);" in your blade(view)
return back()->withErrors($v)->withInput();
}
Run Code Online (Sandbox Code Playgroud)
我在 Laravel 5.3 中像这样处理验证异常。如果您使用 Laravel Collective,它会自动在输入旁边显示错误,如果您使用 laracasts/flash,它还会显示第一个验证错误作为通知。
Handler.php
使成为:
public function render($request, Exception $e)
{
if ($e instanceof \Illuminate\Validation\ValidationException) {
return $this->handleValidationException($request, $e);
}
(..)
}
Run Code Online (Sandbox Code Playgroud)
和功能:
protected function handleValidationException($request, $e)
{
$errors = @$e->validator->errors()->toArray();
$message = null;
if (count($errors)) {
$firstKey = array_keys($errors)[0];
$message = @$e->validator->errors()->get($firstKey)[0];
if (strlen($message) == 0) {
$message = "An error has occurred when trying to register";
}
}
if ($message == null) {
$message = "An unknown error has occured";
}
\Flash::error($message);
return \Illuminate\Support\Facades\Redirect::back()->withErrors($e->validator)->withInput();
}
Run Code Online (Sandbox Code Playgroud)
拉拉维尔 5:
return redirect(...)->withInput();
Run Code Online (Sandbox Code Playgroud)
仅用于背部:
return back()->withInput();
Run Code Online (Sandbox Code Playgroud)
您可以使用这两个中的任何一个:
return redirect()->back()->withInput(Input::all())->with('message', 'Some message');
或者,
return redirect('url_goes_here')->withInput(Input::all())->with('message', 'Some message');
归档时间: |
|
查看次数: |
88100 次 |
最近记录: |