В Laravel "return Redirect :: back () -> withErrors ($ Validator) -> withInput ();" Не работает для меня? - PullRequest
1 голос
/ 26 февраля 2020

Здесь, когда я заполняю поле неверными данными, этот код выдает ошибки, но старый ввод не отображается. Есть ли какое-либо решение ??

  • {{old ('name')}} работает, но я хочу сделать с помощью функцииputput.

Мой контроллер: -

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Userlist;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;

class UserlistController extends Controller
{
    public function index(){
        return view('Project01.index');
    }

    public function store(Request $request){

        $Validator = $request->validate([
            'username' => 'required|unique:userlists,username|max:20',
            'email' => 'required|email|unique:userlists,email|max:20',
            'password' => 'required',
            'bod' => 'required',
            'comments' => 'required',
            'Phone_no' => 'required',
            'gender' => 'required',
            'agreement' => 'required',
        ],[
            'username.required' => 'Please Enter Your Username',
            'email.required' => 'Please Enter Your Username',
            'password.required' => 'Please Enter Your Username',
            'bod.required' => 'Please Enter Your Username',
            'comments.required' => 'Please Enter Your Username',
            'Phone_no.required' => 'Please Enter Your Username',
        ]);

        if($Validator->fails()){
            return Redirect::back()->withErrors($Validator)->withInput();
        }

    }

}

Index.blade. php Страница

@extends('Structure.main2')

@push('Extra-Header-Links')
    <title>Project Index Page</title>

    <style type="text/css">
        .error-msg{
            color: red !important;  
        }
    </style>

@endpush

@section('main-content')
    <div class="container">
        <div class="col-lg-12 text-center mt-3 mb-3 border-bottom" >
            <h1>Project01 Index</h1>
        </div>
    </div>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-8 mb-5">
            <form method="POST" action="{{ route('store') }}">
                    @csrf
                    <div class="form-group">
                        <label for="UserName"><b>UserName</b></label>
                        <input type="text" name="username" class="form-control" id="UserName" value="">
                        @error('username')
                            {{ $errors->first('username') }}
                        @enderror
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1"><b>Email address</b></label>
                        <input type="email" class="form-control" id="exampleInputEmail1" name="email" >
                        @if($errors->has('email'))
                            <small class="form-text text-muted error-msg"> {{ $errors->first('email') }} </small>
                        @endif
                    </div>
                    <div class="form-group">
                        <label for="exampleInputPassword1"><b>Password</b></label>
                        <input type="password" class="form-control" id="exampleInputPassword1" name="password">
                    </div>
                    <div class="form-group">
                        <label for="bod"><b>Birth Of Date</b></label>
                        <input type="date" class="form-control" id="bod" name="bod">
                    </div>
                    <div class="form-group">
                        <label for="comments"><b>Comments</b></label>
                        <textarea name="comments" class="form-control" id="comments" cols="30" rows="5"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="Phone_no"><b>Phone No</b></label>
                        <input type="number" class="form-control" name="Phone_no" id="phone_no">
                    </div>
                    <div class="form-group">
                        <label ><b>Gender :-  </b></label>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="gender" id="inlineradio1" value="option1" checked>
                            <label class="form-check-label" for="inlineradio1">Male</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="gender" id="inlineradio2" value="option2">
                            <label class="form-check-label" for="inlineradio2">Female</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="gender" id="inlineradio3" value="option3">
                            <label class="form-check-label" for="inlineradio3">Other</label>
                        </div>
                    </div>
                    <div class="form-group form-check">
                        <input type="checkbox" class="form-check-input" id="exampleCheck1" value="1" name="agreement">
                        <label class="form-check-label" for="exampleCheck1">Accept All Conditions</label>
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>    
            </div>
        </div>
    </div>
@endsection

@push('Extra-Footer-Scripts')

@endpush

Маршрут: -

Route::prefix('project01')->group(function (){

    Route::get('/', 'UserlistController@index');

    Route::post('create', 'UserlistController@store')->name('store');

});

Пожалуйста, дайте мне знать, если кто-нибудь знает решение этой проблемы , я не знаю, почему это не работает для меня.

Ответы [ 3 ]

1 голос
/ 26 февраля 2020

передать аргумент withInput () $ request-> all (), как это

return Redirect::back()->withErrors($Validator)->withInput($request->all());
0 голосов
/ 26 февраля 2020

используйте Validator для обработки ошибок - проверьте это - LINK

use Validator;
$validator = Validator::make($request->all(),[
            'username' => 'required|unique:userlists,username|max:20',
            'email' => 'required|email|unique:userlists,email|max:20',
            'password' => 'required',
            'bod' => 'required',
            'comments' => 'required',
            'Phone_no' => 'required',
            'gender' => 'required',
            'agreement' => 'required',
        ],[
            'username.required' => 'Please Enter Your Username',
            'email.required' => 'Please Enter Your Username',
            'password.required' => 'Please Enter Your Username',
            'bod.required' => 'Please Enter Your Username',
            'comments.required' => 'Please Enter Your Username',
            'Phone_no.required' => 'Please Enter Your Username',
        ]);

        if($validator->fails()){
            return Redirect::back()->withErrors($validator)->withInput();
        }

EDITED

if($validator->fails()){
   return Redirect::back()->withErrors($validator)->withInput($request->all());

   //change order if not work above 
   //return Redirect::back()->withInput($request->all())->withErrors($validator);
}
0 голосов
/ 26 февраля 2020
    $Validator = $request->validate([
                'username' => 'required|unique:userlists,username|max:20',
                'email' => 'required|email|unique:userlists,email|max:20',
                'password' => 'required',
                'bod' => 'required',
                'comments' => 'required',
                'Phone_no' => 'required',
                'gender' => 'required',
                'agreement' => 'required',
            ],[
                'username.required' => 'Please Enter Your Username',
                'email.required' => 'Please Enter Your Username',
                'password.required' => 'Please Enter Your Username',
                'bod.required' => 'Please Enter Your Username',
                'comments.required' => 'Please Enter Your Username',
                'Phone_no.required' => 'Please Enter Your Username',
            ]);

      $validator->validate();

Попробуйте это

...