I
I
Ivan Kondratiev2016-06-21 15:33:47
Laravel
Ivan Kondratiev, 2016-06-21 15:33:47

How to get error messages and data from a post after a redirect?

All good. Tell me how to get error messages in the view after redirect
Here is an example

public function registrationTokenSave( Request $request , $token ){
        $oEntryUser = $this->checkToken( $token );

        $validator = Validator::make( $request->all() , $this->rules );

        if ( $validator->fails() ){
            return redirect()->back()
                ->withInput()
                ->withErrors($validator->errors());
        }
        return view('test');
    }

how do i get these errors out
->withInput()
->withErrors($validator->errors());

and besides, the old() helper always returns zero
Tell me what the problem is.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton, 2016-06-23
@Yadalay

At the beginning of the file we write:
And the message can be displayed like this:

public function registrationTokenSave( Request $request , $token ){
    $oEntryUser = $this->checkToken( $token );

    $validator = Validator::make( $request->all() , $this->rules );

    if ( $validator->fails() ){
        Session::flash('errors', $validator->errors()); // Разовый показ сообщения.
        return redirect()->back()
            ->withInput()
            ->withErrors($validator->errors());
    }
    return view('test');
}

And in the template file, for example, use the following code:
@if (session('errors'))
    @foreach(session('errors') as $err)
        <div class="alert alert-danger alert-dismissible fade in" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">×</span>
            </button>
            <strong>{{ $err }}</strong>
        </div>
    @endforeach
@endif

But in your example, errors in the template should be displayed. They are output in this way:
@if (count($errors) > 0)
    @foreach($errors->all() as $error)
        <div class="alert alert-danger alert-dismissible fade in" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">×</span>
            </button>
            <strong>{{ $error }}</strong>
        </div>
    @endforeach
@endif

Getting errors - $errors->all()

V
Vlad, 2016-06-27
@Result007

Hey! I hope the question is still relevant.
I'm using a basic error output construct:

<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
    <label class="col-md-4 control-label">Имя:</label>
    <div class="col-md-6">
        <input type="text" class="form-control" name="name" value="{{ old('name') }}">

        @if ($errors->has('name'))
            <span class="help-block">
                <strong>{{ $errors->first('name') }}</strong>
            </span>
        @endif
     </div>
</div>

If you are using the form constructor, then the input should be written without using old() :
{!! Form::text('name', null, array('class' => 'form-control input-sm')) !!}

D
David, 2016-06-21
@MiragePresent

I do like this:
{{ Request::old('email')}}- value

{{ ($errors->has('email') ?  $errors->first('email') : '') }}
- mistake

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question