0

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 class="container">
        @if ($errors->any())
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
        <form action="{{route('client.store')}}" method="post">
            @csrf
            <div class="row mt-3">
                <div class="col-12 col-md-6">
                    <div class="mb-3">
                        <label for="name" class="form-label">Nome</label>
                        <input type="text" class="form-control p-3" name="name" required>
                    </div>
                    <div class="mb-3">
                        <label for="surname" class="form-label">Cognome</label>
                        <input type="text" class="form-control p-3" name="surname" required>
                    </div>
                    <div class="col-12">
                        <div class="mb-3">
                            <label for="diagnosis" class="form-label">Diagnosi</label>
                            <input type="text" class="form-control p-3" name="diagnosis" required>
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary mb-5 py-3 px-5 mt-3 ms-3">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

22
  • What is the result you are getting, and what is the result you are expecting? Commented Oct 5, 2022 at 17:14
  • @RobBiermann for example, if I don't put the name, since it is obligatory, I should expect the error (since the name field is required). Instead nothing is displayed Commented Oct 5, 2022 at 17:16
  • Ok, to be clear, by 'nothing', you mean a white screen? Commented Oct 5, 2022 at 17:19
  • @RobBiermann yes, I stay on the form without displaying (above the form) the error as I requested in the view Commented Oct 5, 2022 at 17:20
  • did u try to put it in a variable and then dd it? $validated = $request->validate... and then dd it after create to see what you get back... Commented Oct 5, 2022 at 17:33

1 Answer 1

1

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

<div class="mb-3">
    <label for="name" class="form-label">Nome</label>
    <input type="text" class="form-control p-3" name="name" required>
</div>
<div class="mb-3">
    <label for="surname" class="form-label">Cognome</label>
    <input type="text" class="form-control p-3" name="surname" required>
</div>
<div class="col-12">
    <div class="mb-3">
        <label for="diagnosis" class="form-label">Diagnosi</label>
        <input type="text" class="form-control p-3" 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.

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

1 Comment

perfect, thank you infinitely

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.