Home > Back-end >  Laravel: Display of validation errors not shown
Laravel: Display of validation errors not shown

Time:10-06

I'm having a problem viewing validation errors in the blade view; this is the code below.

Controller (ClientController)

public function store(Request $request) {
    $request->validate([
        'name' => 'required',
        'surname' => 'required',
        'diagnosis' => 'required',
    ]);

    Client::create([
        'name'=>$request->name,
        'surname'=>$request->surname,
        'city'=>$request->city,
        'diagnosis'=>$request->diagnosis,
    ]);
    return redirect(route('client.index'))->with('message','The customer was successfully saved');
}

View (client.create)

<x-layout>
    <div >
        @if ($errors->any())
            <div >
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
        <form action="{{route('client.store')}}" method="post">
            @csrf
            <div >
                <div >
                    <div >
                        <label for="name" >Nome</label>
                        <input type="text"  name="name" required>
                    </div>
                    <div >
                        <label for="surname" >Cognome</label>
                        <input type="text"  name="surname" required>
                    </div>
                    <div >
                        <div >
                            <label for="diagnosis" >Diagnosi</label>
                            <input type="text"  name="diagnosis" required>
                        </div>
                    </div>
                    <button type="submit" >Add</button>
                </div>    
            </div>
        </form>
    </div>
</x-layout>

I have followed the documentation but am unable to understand where the problem is.

Laravel documentation

Thanks to those who will help me

CONTROLLER UPDATE:

 /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('client.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'surname' => 'required',
            'diagnosis' => 'required',
        ]);
        
        //dd($request->all());

        Client::create([
            'name'=>$request->name,
            'surname'=>$request->surname,
            'city'=>$request->city,
            'diagnosis'=>$request->diagnosis,
            'stay'=>$request->stay
        ]);
        return redirect(route('client.index'))->with('message','The customer was successfully saved');
    }

Index is a blade view that contains the customer table (this works fine). The problem is the error messages I would like to see in the create view if an input is required and not compiled

CodePudding user response:

So after checking all components, it has been under our nose the whole time. All your inputs have the required attribute:

<div >
    <label for="name" >Nome</label>
    <input type="text"  name="name" required>
</div>
<div >
    <label for="surname" >Cognome</label>
    <input type="text"  name="surname" required>
</div>
<div >
    <div >
        <label for="diagnosis" >Diagnosi</label>
        <input type="text"  name="diagnosis" required>
    </div>
</div>

This way the request is not sent, because the browser actively needs to fulfil all requirements to start the request to client.create

If you would remove one of these attributes and then not fill it in and submit, it will cause the errors to show.

However, we concluded that it is better to keep the required attribute in, as it is better to prevent a call to the webserver than to only let laravel do the work of validation.

The laravel validation is more useful for ajax/api calls, where there is no frontend to prevent you from making the request, like this:

//required jquery
$.ajax({
    url: '/your/url/here',
    method: 'POST',
    data: [
        name: 'somename',
        surname: 'somesurname',
    ],
    success(response) {
       console.log('Yay, it succeeded')
    },
    error(error) {
    //I havent worked with jquery in a while, the error should be in error object
      console.log(error);
    }
})

Or how I like to do it in vue, with axios:

//requires axios
axios
.post('/url/here', {
    surname: 'somesurname',
    diagnosis: 'somediagnosis',
})
.then(response => {
    console.log('Yay, it succeeded')
})
.catch(error => {
    console.log('Error', error)
})

You can see in the last two examples, as there is no frontend to prevent this request from being made, you now at least make sure laravel is not going to run it's logic with missing variables, which would cause a crash.

  • Related