Laravel验证似乎根本不起作用

时间:2018-03-06 11:10:20

标签: php laravel laravel-validation

我正在尝试验证表单数据,就像在laravel的默认身份验证中一样。它几天前工作,但现在它不起作用。如果用户没有在表单中出现任何错误并提交,那么它会成功地将数据保存在数据库中。 如果用户提交没有任何数据的空表单或在表单中出现错误,则表示没有显示错误消息。如果我在try catch中添加控制器功能代码,则异常显示为“无效数据”

视图(形式)的

  <form class="form-horizontal" action="{{ route('save-service-page-content') }}" method="POST" enctype="multipart/form-data">
     <div class="box-body">
          {{ csrf_field() }}
         <div class="form-group{{ $errors->has('file') ? ' has-error' : '' }}">
           <label for="image" class="col-sm-2 control-label">Service Image</label>
           <input hidden id="service_image_file" name="file"/>
           <div class="col-sm-10" id="demo-upload">
             <div class="dropzone needsclick dz-clickable" id="serviceImageUpload">
               <div class="dz-default dz-message">
                  <i class="fa fa-image fa-5x"></i>
                  <h3 class="sbold">Drop an image here to upload</h3>
                  <span>You can also click to open file browser</span>
              </div>
             </div>
             @if ($errors->has('file'))
              <span class="help-block"><strong>The service image is reuired</strong></span>
             @endif
           </div>
         </div>
         <div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}">
           <label for="inputEmail3" class="col-sm-2 control-label">Description</label>
           <div class="col-sm-10">
             <textarea rows="8" class="form-control" name="description" placeholder="Description goes here"></textarea>
             @if ($errors->has('description'))
              <span class="help-block"><strong>{{ $errors->first('description') }}</strong></span>
             @endif
           </div>
         </div>
         <div class="form-group{{ $errors->has('description_sin') ? ' has-error' : '' }}">
           <label for="inputEmail3" class="col-sm-2 control-label">හැදින්වීම</label>
           <div class="col-sm-10">
             <textarea rows="8" class="form-control" name="description_sin" placeholder="හැදින්වීම සිංහලෙන්"></textarea>
              <small class="form-text text-muted">හැදින්වීම ඇතුලත් කරන්න. (හැදින්වීම සිංහල බසින් ඇතුලත් කලොත් පමණක් එය ඉංග්‍රීසි බස වෙනුවට සිංහල බසින් දිස්වනු ඇත.)</small>
              @if ($errors->has('description_sin'))
               <span class="help-block"><strong>මෙම හැදින්වමෙහි අක්ෂර සහ ඉලක්කම් පමණක් ඇතුලත් විය යුතුය </strong></span>
              @endif
           </div>
         </div>
     </div>
     <!-- /.box-body -->
     <div class="box-footer clearfix">
       <button type="submit" class="btn btn-default">Cancel</button>
       <button type="submit" class="btn btn-info pull-right">Post</button>
     </div>
   </form>

控制器

namespace App\Http\Controllers;
use App\Service_page_content;
use App\Service;
use File;
use Image;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ServiceContent extends Controller
{
  protected function validator(array $data)
 {
   return Validator::make($data, [
      'file' => 'required',
      'description' => 'nullable|alpha_num_spaces_brackets',
      'description_sin' => 'nullable|alpha_num_spaces_brackets',
    ]);
 }


  public function save_page_content(Request $request)
  {
  $this->validator($request->all())->validate();
  $service_page_content = new Service_page_content;
  $service_page_content->description = $request->description;
  $service_page_content->description_sin = $request->description_sin;

  $file = $request->file;
  $image_decode = base64_decode($file);
  $image_data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $file));
  $f = finfo_open();
  $mime_type = finfo_buffer($f, $image_data, FILEINFO_MIME_TYPE);
  $imageName = "service-page-content-".time().'.'.str_replace("image/","",$mime_type);
  $image_resized = Image::make($image_data);
  $image_resized->resize(1170, null, function ($constraint) {
    $constraint->aspectRatio();
  });
  $image_resized->save(public_path("uploads/service_page_content_uploads/$imageName"));

  $service_page_content->main_img_url = $imageName;
  $service_page_content->save();
  return redirect()->back()->with('success', ['Data Saved']);
 }
}

我不知道我是否在return Validator::make($data,......$this->validator($request->all())->validate();上正确地执行了此操作

我编写了一个自定义验证规则,允许使用字母数字,空格,括号,'。'和','在AppServiceProvider启动函数中。它也在几天前工作。现在似乎没什么用。

几天前就开始工作了。忽略它正在运行的文件上传部分我正在使用Dropzone.js。可能是我错过了什么。任何帮助,将不胜感激 !

1 个答案:

答案 0 :(得分:0)

您可以直接在阵列上进行验证,而无需调用Validator外观。

 protected function validator(Request $request)
 {
    return $request->validate([
      'file' => 'required',
      'description' => 'nullable|alpha_num_spaces_brackets',
       'description_sin' => 'nullable|alpha_num_spaces_brackets',
    ]);
 }

public function save(Request $request){
    $this->validator($request);
}