Daniel Barde
Daniel Barde

Reputation: 2703

Laravel Message Bag Error

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!

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>

Upvotes: 8

Views: 37865

Answers (4)

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 view:

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

Upvotes: 10

fmgonzalez
fmgonzalez

Reputation: 823

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

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

I hope it works fine for you.

Upvotes: 1

Bruce Tong
Bruce Tong

Reputation: 1431

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

Upvotes: -1

Sven van Zoelen
Sven van Zoelen

Reputation: 7229

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

The function 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 MessageBag then this should work (didn't test it):

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

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

Upvotes: 8

Related Questions