3

I want only error messages not the field with codes

Controller

 $validator = Validator::make(request()->all(),[
        'txtName' =>'required|max:25',
       ]);

     if($validator->fails()) {
            $errors = $validator->errors();
            return redirect()->back()->with('error', $errors);
        }

View:

@if(session()->has('error'))
{!! \Session::get('error') !!}
@endif

Return:

{"txtName":["The txt name may not be greater than 25 characters."]}

What I need:

name may not be greater than 25 characters

2 Answers 2

4

First instead of with use withErrors

 if($validator->fails()) {
            $errors = $validator->errors();
            return redirect()->back()->withErrors($errors);
        }

and in view

@if($errors->any())
     <div class="alert alert-danger">
          <ul class="list-unstyled">
                 @foreach ($errors->all() as $error)
                       <li>{{ $error }}</li>
                 @endforeach
          </ul>
      </div>
 @endif
Sign up to request clarification or add additional context in comments.

1 Comment

Illegal offset type
1

Use @foreach.

@if(session()->has('error'))

   @foreach($errors->all() as $error as $error)
      <p>{{ $error }}</p>
   @endforeach

@endif

3 Comments

it returns nothing
Try @foreach($errors->all() as $error) in your view
No. I followed the accepted answer it worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.