0

I'm new to Laravel, here I'm working with Laravel project. I need to display validation errors in form

<div class="col s12 m6">
    <div class="row">
        <div class="col s12 input-field">
            <input name="newpassword" id="newpassword" type="password" class="validate">
            <label for="newpassword"> New Password </label>
            <small class="email_error"><div class="error">@error('newpassword'){{$message}}@enderror</div></small>
        </div>
    </div>
</div>

Controller

$this->validate($request, [
    'newpassword' => 'required|min:8',
], [
    'newpassword.required' => 'New Password is required field.',
    'newpassword.min:8'    => 'Enter Minimum 8 Characters',
]);

errors :

enter image description here

5
  • and what is your question? you have a JSON response, everything seems to be in working order, you just have to do something with that response Commented Dec 21, 2020 at 8:27
  • Please can you show the javascript you're using to submit the form. Commented Dec 21, 2020 at 8:28
  • return back()->with('error', 'You have entered wrong password'); @Rwd Commented Dec 21, 2020 at 8:46
  • That isn't javascript. If you're getting a json response, I'm assuming you're using some sort of ajax to submit the form? Commented Dec 21, 2020 at 8:54
  • Thanks Bro, because i found my error, i'm use both method ajax or without ajax in one form, i forget to stop ajax @Rwd Commented Dec 21, 2020 at 8:59

2 Answers 2

2

The easiest way would be to make a shared blade file, which you can use for any error handling (sessions, requests & etc..). The file would look something like this:

@if ( session()->has('success') )
    <div class="row mb-3">
        <div class="col-sm-12">
            <div class="alert text-white" style="background-color: #47afc4;     border-radius:5px;">
                {!! session('success') !!}
            </div>
        </div>
    </div>
@endif
@if ( session()->has('error') )
    <div class="row">
        <div class="col-sm-12">
            <div class="alert alert-danger">
                {!! session('error') !!}
            </div>
        </div>
    </div>
@endif
@if (count($errors) > 0)
    <div class="row">
        <div class="col-sm-12">
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        </div>
    </div>
@endif

The session()->has('success') would catch any success session messages that you return in you controller:

return redirect('your route')->with('success', 'Success message');

The session()->has('error') would catch any error session messages that you return in you controller:

return redirect('your route')->with('error', 'Error message');

And $errors->all() will return any errors from the request validation.

You can then use this file in the pages you want, simply include it:

@inlcude('path.to.shared.errors.file')

If you need any more info, let me know.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use $errors->any() function

To find your answer look at this :

Displaying the Error Messages in Laravel after being Redirected from controller

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.