php 向 $error MessageBag 添加一条消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25437455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a message to the $error MessageBag
提问by Jason
This is Laravel 4.2
这是 Laravel 4.2
Laravel will make the $error MessageBag available to views. This can be populated with flash messages in the previouspage by redirecting ->withErrors()
. That is fine if you are redirecting to a new page.
Laravel 将使 $error MessageBag 可用于视图。这可以通过重定向在上一页中填充 Flash 消息->withErrors()
。如果您要重定向到新页面,那很好。
return Redirect::route('my_route')->withErrors($validator);
I am generating some errors in the controller, not validating a form, and want to put those messages into the $errors MessageBag that Laravel automatically passes into the views. But how? The MessageBag is somewhere, but how do I get to it, and how do I add some messages to it to display in the currentpage?
我在控制器中生成了一些错误,而不是验证表单,并希望将这些消息放入 Laravel 自动传递到视图中的 $errors MessageBag 中。但是如何?MessageBag 在某处,但我如何获得它,以及如何向其中添加一些消息以显示在当前页面中?
回答by Jason
It looks like I can inject messages when the view is created.
看起来我可以在创建视图时注入消息。
My controller passes the required view, with the data it collects, to the content of the page template like this:
我的控制器将所需的视图及其收集的数据传递给页面模板的内容,如下所示:
$this->layout->content = View::make('my.view', $view_data);
Errors can be passed in like this:
错误可以这样传入:
$this->layout->content = View::make('my.view', $view_data)->withErrors($my_errors);
where $my_errors can be null or array() (for "no additional errors"), an array of text messages, or a MessageBag.
其中 $my_errors 可以是 null 或 array()(表示“没有其他错误”)、文本消息数组或 MessageBag。
My outer page template then just picks up the messages if they exist, and displays them at the top of the page:
然后,我的外页模板仅选取存在的消息,并将它们显示在页面顶部:
@if ( $errors->count() > 0 )
...An error occured...
@foreach( $errors->all() as $message )
...{{ $message }}...
@endforeach
@endif
(with markup where appropriate)
(在适当的地方带有标记)
回答by Luís Cruz
I have done that with some custom validations. What you need is something like this:
我已经通过一些自定义验证做到了这一点。你需要的是这样的:
use Illuminate\Support\MessageBag;
class MyCustomValidator
{
protected $errors = array();
protected $messageBag;
public function __construct(MessageBag $messageBag)
{
$this->messageBag = $messageBag;
}
public function setErrors($errors = array())
{
for($i = 0; $i < count($errors); $i++) {
$this->messageBag->add($i, $errors[$i]);
}
$this->errors = $this->messageBag;
}
public function getErrors()
{
return $this->errors->all();
}
}
In your controller you have to call something like
在您的控制器中,您必须调用类似
$validator = new MyCustomValidator();
$validator->getErrors();
You can access the full documentation for the MessageBag in the docs.
回答by Han Hermsen
Laravel 5
Laravel 5
$validator = .......
$validator->after(function($validator) {
.....
$validator->errors()->add('tagName', 'Error message');
.....
});
Mind that the anonimous function is in the scope of the class, not in that of the function where you use it. If you need the value of some variable from the outside world it must be a class variable!
请注意匿名函数在类的范围内,而不是在您使用它的函数的范围内。如果你需要外部世界的某个变量的值,它必须是一个类变量!
回答by Chintan7027
Be late but another solution to add custom message in error message bag is:
迟到但在错误消息包中添加自定义消息的另一种解决方案是:
Controller
控制器
$rules = array(
'email' => 'required|exists:users,email|email|max:32',
'password' => 'required|max:20'
);
$validator = Validator::make($request->all(),$rules);
$email = $request->email;
$password = $request->password;
$validateUser = new user();
$users = $validateUser::where('email', $email)->get();
if($users->isEmpty()){
$validator->getMessageBag()->add('email', 'Invalid Email Address');
return redirect('home')->withErrors($validator);
}
foreach ($users as $user) {
$data = $user->showAdminData();
if($user->role_id!=1){
$validator->getMessageBag()->add('email', 'Unauthorised access');
}
if(Crypt::decrypt($user->password)!==$password){
$validator->getMessageBag()->add('password', 'Invalid Password');
}
}
return redirect('home')->withErrors($validator);
View
看法
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>