Home > Mobile >  Error on input validation even though input exists
Error on input validation even though input exists

Time:01-10

I have an application where an user can change his name via a pop-up.

This is the method that handles the name change:

public function changeVorname(Request $request) {
        $this->validate($request, [
            'vorname' => 'required|max:255',
        ]);

        Portal::query()->where('email', $request->email)->update(['vorname' => $request->neuer_vorname]);

        return redirect()->back()->with('message', 'Vorname erfolgreich geändert');
    }

This is the blade file with the modal:

 <div >
                            <p>Vorname:</p>
                            <input type="text" name="vorname" id="vorname"
                                   value="{{ \Illuminate\Support\Facades\Auth::guard('portal')->user()->vorname }}"
                                   
                                   readonly>
                            @error('vorname')
                            <div >
                                {{ 'Der Vorname darf keine Zahlen enthalten und nicht leer sein' }}
                            </div>
                            @enderror
                            <div >
                                <button id="change_vorname"  name="change_vorname">
                                    Vorname bearbeiten
                                </button>
                            </div>
                        </div>


                        <div id="change_vorname_modal"
                             >
                            <div >
                                Neuer Vorname
                            </div>

                            <div >
                                <label for="neuer_vorname" >Neuer Vorname</label>

                                <form
                                    action="{{ route('change_vorname', \Illuminate\Support\Facades\Auth::guard('portal')->user()->email) }}"
                                    method="post">
                                    @csrf
                                    <input type="text" name="neuer_vorname" id="neuer_vorname"
                                           placeholder="Neuer Vorname" value=""
                                           >
                                    <button type="submit" id="ok_btn" >
                                        Vorname ändern
                                    </button>
                                </form>
                            </div>

                            <div >
                                <button id="back" >
                                    zurück
                                </button>
                            </div>
                        </div>
                        @if(session()->has('message'))
                            <div >
                                {{ session()->get('message') }}
                            </div>
                        @endif

                        <script>
                            var vorname_modal = document.getElementById("change_vorname_modal");


                            var vorname_btn = document.getElementById("change_vorname");

                            var back_btn = document.getElementById("back");

                            vorname_btn.onclick = function () {
                                vorname_modal.style.display = "block";
                            }

                            back_btn.onclick = function () {
                                vorname_modal.style.display = "none";
                            }

                            window.onclick = function (event) {
                                if (event.target == modal) {
                                    vorname_modal.style.display = "none";
                                }
                            }
                        </script>

The issue is when I have the required in the request validation I always get the error, when I delete the required everything works perfectly. I need it because a user should not be able to rename his name to an empty one.

CodePudding user response:

You have a mistake on name of new name input. its not same as validators key
change your validator like this

$this->validate($request, [
    'neuer_vorname' => 'required|max:255',
]);

it will work

  •  Tags:  
  • Related