Laravel自定义错误消息,提交数据并重定向到同一页面后出现一些错误信息

时间:2018-11-13 11:32:56

标签: php html laravel

所以基本上我的计划是使我的应用程序更简单。如果用户想借很多书,那可能是该用户再次扫描同一本书,而我得到的页面如下:

https://gyazo.com/e7236fec2cfc745d604d7eced2a1e333

或使用此:

enter image description here

我知道这是laravel的生产方式而不是发展方式。那么,如何为该页面重定向错误信息的页面,使用户能够理解自己已经扫描过两次书了?因此,我只想从数据库发出的错误消息转换为简单的重定向和消息,但是我如何实现此目标?

起初,我给你我的视图代码:

      <div class="row justify-content-center">
        <div class="col-md-8">
    <!--  <script> setTimeout(function(){window.location=localhost:8000/}, 5); </script> -->
          <br>
          <br>
            <div class="card">
                <div class="card-header">{{ __('lend book') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('borrow.store') }}">
                        @csrf

                        <div class="form-group row">
                            <label for="serialnumber" class="col-md-4 col-form-label text-md-right">{{ __('Bitte serialnumber scannen') }}</label>

                            <div class="col-md-6">
                                <input id="serialnumber" type="text" class="form-control{{ $errors->has('serialnumber') ? ' is-invalid' : '' }}" name="serialnumber" value="{{ old('serialnumber') }}" required autofocus>

                                @if ($errors->has('serialnumber'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('serialnumber') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                          <div class="form-group row">
                            <label for="ma_id" class="col-md-4 col-form-label text-md-right">{{ __('scan membercard to identify yourself') }}</label>
                            <div class="col-md-6">

                              <input id="ma_id" type="password" class="form-control" name="ma_id" required>


                              @if ($errors->has('ma_id'))
                              <span class="invalid-feedback">
                                <strong>{{ $errors->first('ma_id') }}</strong>
                              </span>
                              @endif
                          </div>
                        </div>


                        <div class="form-group row">
                          <label for="comment" class="col-md-4 col-form-label text-md-right">{{ __('comment') }}</label>
                          <div class="col-md-6">

                            <input id="comment" type="text-field" class="form-control" name="comment" placeholder="Test">


                            @if ($errors->has('comment'))
                            <span class="invalid-feedback">
                              <strong>{{ $errors->first('comment') }}</strong>
                            </span>
                            @endif
                        </div>
                        </div>




                      <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('send') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
 @endsection

这是我的控制器代码:

 public function store(Request $request)
{
    $this->middleware('guest');

    request()->validate([
      'seriennummer' => 'required',
      'ma_id' => 'required'
    ]);

    borrow::create($request->all());


    return redirect()->route('borrow.index')
                     ->with('success','Proccess succesfully');

}

所以我试图将返回值更改为以下内容:

redirect()->back()->withErrors($validator)->withInput();

 return back()->with('autofocus', true);

和类似的视图:

<input id="serialnumber" type="text" class="form-control{{ $errors->has('serialnumber') ? ' is-invalid' : '' }}" name="serialnumber" value="{{ old('serialnumber') }}" required   @if (Session::has('autofocus')) autofocus @endif>

有人可以帮助我解决这个问题吗?还是有人知道如何解决此问题?

2 个答案:

答案 0 :(得分:1)

在您的根项目文件夹(CMD)中运行php artisan make:request BookRequest

将新请求添加到您的控制器中的函数中,因此它将如下所示:

use App\Http\Requests\BookRequest

Class BookController extends Controller
{
    ...      

    public function store (BookRequest $request)
    {
        <Your code here, you do not need to worry about validation, it is done previous of calling store now! If it fails it redirects automatically to the previous page!>
        ... Your logic to add the book into the DB...
    }
    ...
}

位于App\Http\Requests BookRequest类

Class BookRequest extends FormRequest
{
    public function authorize()
    {
        <You can add here authorization like if user is authenticated. If you do not wish so, and just make it available just return TRUE>

        return TRUE;
    }        

    public function rules()
    {
        return [
            'seriennummer' => 'required|unique',
            'ma_id' => 'required'
        ];
    }

    public function messages()
    {
        return [
            'seriennummer.required' => 'You need to select a book',
            'seriennummer.unique' => 'Seems like you have added the same book more than once!',
        ... Your other fields you wish to validate. This fields are declared on the form!...
       ];
    }

}

在您的视图上:

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

答案 1 :(得分:0)

尝试一下:

use Illuminate\Support\Facades\Validator;
...
$validator = Validator::make($request->all(), [
   'seriennummer' => 'required|unique',
   'ma_id' => 'required'
]);
if ($validator->fails()) {
    return redirect()->back()
                    ->withErrors($validator)
                    ->withInput();
}

并在视图上

@if ($errors->has('seriennummer'))
    <span class="help-block"><strong>{{ $errors->first('seriennummer') }}</strong></span>
@endif