控制器
public function store(Request $request) { $validator = Validator::make($request->all(), [ 'Subject.title' => 'required|string', 'Subject.subtitle' => 'string', 'Subject.image' => 'required|file|image', 'Subject.content' => 'required|string', 'Subject.product_ids' => 'array', ], [ 'title.required' => '标题不能为空', 'image.required' => '图片为空或格式出错', 'content.required' => '专题内容不能为空', ],[ 'Subject.title' => '标题', 'Subject.image' => '图片', 'Subject.content' => '专题内容' ]); if ($validator->fails()) { $errors = $validator->errors()->toArray(); return redirect()->back()->withInput()->with(compact('errors')); }
。。。。
可以用with(compact('errors') 用了返回错误,在页面上显示,用withError()方法,试了下,没有效果的。
视图
<section> <div class="section-body"> <div class="card"> <div class="card-body"> <div class="row"> @php $subject_exist = isset($subject) ? true : false; @endphp @if($subject_exist) {!! Form::open(['action' => ['AdminSubjectController@update', $subject->id], 'class' => 'form', 'id' => 'subject-form', 'enctype' => 'multipart/form-data', 'method' => 'PUT']) !!} @else {!! Form::open(['action' => 'AdminSubjectController@store', 'class' => 'form', 'id' => 'subject-form', 'enctype' => 'multipart/form-data']) !!} @endif @include('admin.widget.image-upload', [ 'colsm' => '4', 'collg' => '2', 'id' => 'subject-image', 'name' => 'image', ]) @include('admin.widget.input', [ 'name' => 'editIden', 'id' => 'editIden', 'type' => 'hidden' ]) @include('admin.widget.input', [ 'colsm' => '12', 'collg' => '8', 'name' => "Subject[title]", 'id' => 'title', 'title' => '标题', 'type' => 'text', 'value' => old('Subject')['title'] ? old('Subject')['title'] : ( isset($subject) && $subject->content ? $subject->title : '' ) //两个三元运算符的一起写的,切记加括号() ]) @include('admin.widget.input', [ 'colsm' => '12', 'collg' => '8', 'name' => "Subject[subtitle]", 'id' => 'subtitle', 'title' => '子标题', 'type' => 'text', 'value' => old('Subject')['subtitle'] ? old('Subject')['subtitle'] : (isset($subject) && $subject->subtitle ? $subject->subtitle : ''), ]) <div class="row"> <div class="col-lg-10 col-sm-12"> @include('admin.widget.textarea', [ 'colsm' => '12', 'collg' => '12', 'name' => "Subject[content]", 'id' => 'summernote', 'title' => '', 'value' => old('Subject')['content'] ? old('Subject')['content'] : (isset($subject) && $subject->content ? $subject->content : ''), ]) </div>