php Laravel 消息包错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24011746/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:06:16  来源:igfitidea点击:

Laravel Message Bag Error

phplaravellaravel-4

提问by Daniel Barde

Am working on form validations for newsletter for a project am on, the news letter form appears on every page so it will also appear on the longin and registration page so i decided to make use of Laravel Message Bags to store the news letter errors but it keeps giving me an undefined property error on the actual page i check and output echo the errors, i don't know if am doing something wrong here are the details though!

我正在为一个项目的时事通讯进行表单验证,时事通讯表单出现在每一页上,所以它也会出现在 longin 和注册页面上,所以我决定使用 Laravel Message Bags 来存储时事通讯错误,但它在实际页面上不断给我一个未定义的属性错误,我检查并输出回显错误,我不知道我是否做错了这里是详细信息!

The Error:

错误:

Undefined property: Illuminate\Support\MessageBag::$newsletter

My code In the Controller:

我在控制器中的代码:

return Redirect::back()->withInput()->withErrors($inputs, "newsletter");

My code in the View:

我在视图中的代码:

 @if($errors->newsletter->any())

 <p>
    {{$errors->newsletter->any()}}
 </p>

回答by Sven van Zoelen

The RedirectResponseclass function withErrors()doesn't have a second parameter..

RedirectResponse类函数withErrors()没有第二个参数..

The function vendor\laravel\framework\src\Illuminate\Http\RedirectResponse.php -> withErrors():

功能vendor\laravel\framework\src\Illuminate\Http\RedirectResponse.php -> withErrors()

/**
 * Flash a container of errors to the session.
 *
 * @param  \Illuminate\Support\Contracts\MessageProviderInterface|array  $provider
 * @return \Illuminate\Http\RedirectResponse
 */
public function withErrors($provider)
{
    if ($provider instanceof MessageProviderInterface)
    {
        $this->with('errors', $provider->getMessageBag());
    }
    else
    {
        $this->with('errors', new MessageBag((array) $provider));
    }

    return $this;
}

So, if you really want to use the MessageBagthen this should work (didn't test it):

所以,如果你真的想使用,MessageBag那么这应该可以工作(没有测试它):

$your_message_bag = new Illuminate\Support\MessageBag;
$your_message_bag->add('foo', 'bar');

return Redirect::back()->withInput()->withErrors($your_message_bag->all());

回答by Сергей Слободинский

Code in controller:

控制器中的代码:

         $post_data = Input::all();

    $validator = Validator::make(Input::all(),
        array(
            'email' => 'required',
            'password' => 'required'
        ));


    if ($validator->fails()) {

        return Redirect::back()
            ->withInput()
            ->withErrors(['auth-validation' => 'ERROR: in validation!']);
    } 

Code in vew:

代码如下:

@if($errors->any())
    @foreach($errors->getMessages() as $this_error)
        <p style="color: red;">{{$this_error[0]}}</p>
    @endforeach
@endif 

回答by fmgonzalez

withErrorsshould receive the messages from validator object. After your validation process something like:

withErrors应该从验证器对象接收消息。在您的验证过程之后,例如:

  $validation = Validator::make(Input::all(), $validation_rules);
  if (!$validation->passes()){
       return Redirect::back()->withInput()->withErrors($validation->messages());
  }

I hope it works fine for you.

我希望它对你有用。

回答by Bruce Tong

You can write a function like this

你可以写一个这样的函数

if(!function_exists('errors_for')) {

 function errors_for($attribute = null, $errors = null) {

    if($errors && $errors->any()) {

        return '<p class="text-danger">'.$errors->first($attribute).'</p>';

    }   

 }

}

then in your View

然后在你的视图中

<div class="form-group">
   <label for="bio">Bio</label>
   <textarea class="form-control" name="bio"></textarea>
</div>
{!! errors_for('bio',$errors) !!}

learnt from Jeffrey Way on Laracasts

在 Laracasts 上向 Jeffrey Way 学习