Laravel 在字段验证后插入记录

我正在尝试 Laravel 5.6 请求验证。我面临几个问题


1.无法查看视图中的验证消息。


2.添加验证码数据后不插入数据库。(添加验证之前它的工作)


3 如何验证下拉状态?(应选择 Active/Inactive)


designation.blade.php


<form action="{{url('./designation/store')}}" method="POST">

      <div class="form-row">

        <div class="form-group col-md-7">

          <label for="inputDesignation">Designation</label>

          <input type="text" name="designation" class="form-control" id="inputDesignation">

        </div>

        <div class="form-group col-md-5">

          <label for="inputStatus_Designation">Status</label>

          <select name="status" id="inputStatus_Designation" class="form-control">

            <option selected>Select Status</option>

            <option >Active</option>

            <option >Inactive</option>

          </select>

        </div>

      </div>


      <button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>

      {{ csrf_field() }}

  </form>

指定控制器.php


public function store(Request $request)

{

    //Validation the Data


    $validatedData = $request->validate([

        'designation_type' => ['required','max:255'],

        'status' => ['required'],

    ],

    [

        'designation_type.required' => 'Designation is required',

        'designation_type.max' => 'Designation should not be greater than 255 characters.',

    ]);


    //Data Insert into database

   $data =[

       'designation_type'=>$request->input('designation'),

       'status'=>$request->input('status')

   ];

   DB::table('designation')->insert($data);


   return redirect('/designation');

}

请帮我解决这个问题!


阿波罗的战车
浏览 126回答 3
3回答

慕斯王

尝试以下代码public function store(Request $request){&nbsp; &nbsp; //Validation the Data&nbsp; &nbsp; $validatedData = $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'designation_type' => ['required','max:255'],&nbsp; &nbsp; &nbsp; &nbsp; 'status' => ['required'],&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'designation_type.required' => 'Designation is required',&nbsp; &nbsp; &nbsp; &nbsp; 'designation_type.max' => 'Designation should not be greater than 255 characters.',&nbsp; &nbsp; ]);&nbsp; &nbsp; if($validatedData->fails()) {&nbsp; &nbsp; &nbsp; return Redirect::back()->withErrors($validatedData);&nbsp; &nbsp; }&nbsp; &nbsp; //Data Insert into database&nbsp; &nbsp;$data =[&nbsp; &nbsp; &nbsp; &nbsp;'designation_type'=>$request->input('designation'),&nbsp; &nbsp; &nbsp; &nbsp;'status'=>$request->input('status')&nbsp; &nbsp;];&nbsp; &nbsp;DB::table('designation')->insert($data);&nbsp; &nbsp;return redirect('/designation');}<form action="{{url('./designation/store')}}" method="POST">&nbsp; &nbsp; &nbsp; <div class="form-row">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group col-md-7">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="inputDesignation">Designation</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="designation" class="form-control" id="inputDesignation">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if($errors->has('designation'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="error">{{ $errors->first('designation') }}</div>&nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group col-md-5">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="inputStatus_Designation">Status</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="status" id="inputStatus_Designation" class="form-control">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option selected>Select Status</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option >Active</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option >Inactive</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if($errors->has('status'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="error">{{ $errors->first('status') }}</div>&nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>&nbsp; &nbsp; &nbsp; {{ csrf_field() }}&nbsp; </form>

慕工程0101907

&nbsp; &nbsp; &nbsp;public function store(Request $request)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->validate($request,[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'required'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $category = new Category();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $category->name = $request->name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $category->slug = str_slug($request->name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $category->save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toastr::success('Category Successfully Saved','Success');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect()->route('admin.category.index');&nbsp; &nbsp; &nbsp; &nbsp; }// blade&nbsp;<form method="POST" action="{{ route('admin.category.store') }}">&nbsp; &nbsp;@csrf&nbsp; &nbsp;<div class="form-group form-float">&nbsp; &nbsp; &nbsp;<div class="form-line">&nbsp; &nbsp; &nbsp;<input value="{{ old('name') }}" name="name" type="text" id="category_name" class="form-control">&nbsp; &nbsp; &nbsp;<label class="form-label">{{ __('Name') }}</label>&nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="{{ route('admin.category.index') }}"&nbsp; class="btn btn-danger m-t-15 waves-effect">{{ __('BACK') }}</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-primary m-t-15 waves-effect">{{ __('SUBMIT') }}</button></form>

慕森卡

参考此https://laravel.com/docs/5.6/validation#named-error-bags后,我对代码进行了一些更改,这有助于解决错误。在designation.blade.php添加&nbsp;@if ($errors->any())<div class="alert alert-danger">&nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; @foreach ($errors->all() as $error)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h6>{{ $error }}</h6>&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </ul></div>@endif在DesignationController.php用于状态下拉验证。'status' => 'required|not_in:0',用于数据插入数据库部分DB::table('designation')->insert($validatedData);完整代码designation.blade.php<div class="row">&nbsp; <div class="col-md-2"></div>&nbsp; &nbsp; <div class="col-md-8">&nbsp; &nbsp; &nbsp; @if ($errors->any())&nbsp; &nbsp; &nbsp; &nbsp; <div class="alert alert-danger">&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; @foreach ($errors->all() as $error)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h6>{{ $error }}</h6>&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </ul></div>@endif</div>&nbsp;<div class="col-md-2"></div>&nbsp;</div>&nbsp;<div class="row"><div class="col-md-4"></div><div class="col-md-4">&nbsp; <form action="{{url('./designation/store')}}" method="POST">&nbsp; &nbsp; &nbsp; <div class="form-row">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group col-md-7">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="inputDesignation">Designation</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="designation_type" class="form-control" id="inputDesignation">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group col-md-5">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="inputStatus_Designation">Status</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="status" id="inputStatus_Designation" class="form-control">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option selected value="">Select Status</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="Active">Active</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="Inactive">Inactive</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; {{-- <button type="submit" class="btn btn-primary">Sign in</button> --}}&nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>&nbsp; &nbsp; &nbsp; {{ csrf_field() }}&nbsp; </form></div>&nbsp;<div class="col-md-4"></div></div>指定控制器.phppublic function store(Request $request){&nbsp; &nbsp; $validatedData = $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'designation_type' => 'required|max:255',&nbsp; &nbsp; &nbsp; &nbsp; 'status' => 'required|not_in:0',&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'designation_type.required' => 'Designation is required !!',&nbsp; &nbsp; &nbsp; &nbsp; 'designation_type.max' => 'Designation should not be greater than 255 characters.',&nbsp; &nbsp; &nbsp; &nbsp; 'status.required' => 'Status is required !!'&nbsp; &nbsp; ]);&nbsp; &nbsp; DB::table('designation')->insert($validatedData);&nbsp; &nbsp; return redirect('/designation');}谢谢你们花费宝贵的时间来帮助我....!!!
打开App,查看更多内容
随时随地看视频慕课网APP