Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 250
Daniel Barde Avatar asked Jun 03 '14 09:06

Daniel Barde


2 Answers

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());
like image 189
Sven van Zoelen Avatar answered Sep 21 '22 18:09

Sven van Zoelen


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 
like image 20
Сергей Слободинский Avatar answered Sep 18 '22 18:09

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