Add message to $ error MessageBag

This is Laravel 4.2

Laravel will make the $ error MessageBag error message available to view. This can be populated with flash messages on the previous page by redirecting ->withErrors(). This is normal if you are redirected to a new page.

return Redirect::route('my_route')->withErrors($validator);

I am generating some errors in the controller without checking the form, and I want to put these messages in the $ Error MessageBag, which Laravel automatically goes into views. But how? A MessageBag is somewhere, but how do I get to it, and how do I add some messages to it to display on the current page?

+4
source share
4 answers

It looks like I can enter messages when the view is created.

, , :

$this->layout->content = View::make('my.view', $view_data);

:

$this->layout->content = View::make('my.view', $view_data)->withErrors($my_errors);

$my_errors null array() ( " " ), MessageBag.

, , :

@if ( $errors->count() > 0 )
    ...An error occured...
    @foreach( $errors->all() as $message )
        ...{{ $message }}...
    @endforeach
@endif

( , )

+5

. - :

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();
    }
}

-

$validator = new MyCustomValidator();
$validator->getErrors();

MessageBag .

+3

Laravel 5

$validator = .......

$validator->after(function($validator) {
    .....               
    $validator->errors()->add('tagName', 'Error message');
    .....
});

, , , , . , !

+1
source

Be late, but another solution is to add a custom message to the bag with the error message:

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>
0
source

All Articles