zoukankan      html  css  js  c++  java
  • Laravel 5.2--如何让表单提交错误,不清空?

    控制器

     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>
  • 相关阅读:
    Javascript函数声明和函数表达式
    浅谈getAttribute兼容性
    js数组去重的三种常用方法总结
    说说JSON和JSONP,也许你会豁然开朗
    web安全之跨站请求伪造
    javascript中对象的深度克隆
    三种方式实现元素水平居中显示与固定布局和流式布局概念理解
    js dom element 属性整理(原创)
    ul下的li浮动,如何是ul有li的高度
    js数组去重的4个方法
  • 原文地址:https://www.cnblogs.com/smallyi/p/6692463.html
Copyright © 2011-2022 走看看