[英]Laravel validation doesn't seem to work at all
我正在尝试像在laravel的默认身份验证中一样验证表单数据。 几天前它可以工作,但是现在不起作用。 如果用户没有在表单上犯任何错误并提交,则它成功地将数据保存在数据库中。 如果用户提交的空表格没有任何数据或在表格中犯了一些错误,则不会显示错误消息。 如果我在尝试捕获中添加控制器功能代码,则异常显示为“无效数据”
视图(窗体)
<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();
时是否做正确$this->validator($request->all())->validate();
我编写了一个自定义验证规则,该规则允许字母数字,空格,方括号,“。” 和AppServiceProvider启动功能中的“,”。 几天前也可以使用。 现在似乎没有任何工作。
它在几天前起作用。 忽略文件上传部分,它工作正常,我正在使用Dropzone.js。 可能是我缺少了一些东西。 任何帮助,将不胜感激 !
您可以直接在数组上进行验证,而无需调用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.