laravel-admin(自定义表单视图)

前言:

在上一遍文章(https://www.cnblogs.com/shiwenhu/p/10271013.html)中写到可以使用自定义form组建来创建表单,几乎能满足我们大部分要求,而且不用我们来写html页面。但是对于很多定制的页面,form组建不能满足我们的要求的页面需求时候,这个时候想自定义一个页面我们手工来定制。

方法:

1.还是在对应的视图方法中,定义我们的视图文件(这个时候定义了我们的test模板,然后传值name初始化,可以用于编辑功能)

    public function create(Content $content)
    {
        // 方式1,使用form组建进行页面渲染
//        $f = new \Encore\Admin\Widgets\Form([
//            'name'=>'初始化数据'
//        ]);
//        $f->action('/adminyc/area');
//        $f->textarea('name', '简介')->help('简介');
//        return $content
//            ->header('Create')
//            ->description('description')
//            ->body($f);

        return $content
            ->header('Create')
            ->description('description')
            ->body(view('test', ['name' => '初始化数据'])->render());
    }

2.对应的test.blade.php

<form action="/adminyc/area" method="post">

    <div class="row">
        <div class="col-md-12">
            <div class="box box-info">
                <div class="box-header with-border">
                    <h3 class="box-title">创建公司</h3>
                </div>
                <div class="form-horizontal">
                    <div class="box-body">
                        <div class="form-group  {!! !$errors->has('name') ? '' : 'has-error' !!}">
                            <label for="text1" class="col-sm-2 control-label">公司名称</label>
                            <div class="col-sm-10">
                                @foreach ($errors->get('name') as $error)
                                    <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$error}}</label><br/>
                                @endforeach
                                <input type="text" class="form-control" id="text1" placeholder="公司名称" name="name" value="{{old('name', $name)}}">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <div class="box-footer">
        <button type="submit" class="btn btn-info pull-right">保存</button>
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
    </div>
</form>

3.对应的业务处理方法

public function store(Request $request)
    {
        $val = \Illuminate\Support\Facades\Validator::make($request->all(),
            [
                'name' => 'required|array'
            ], [
                'name.required' => "名字不能为空"
            ]);
        if ($val->fails()) {
            return redirect('/adminyc/area/create')
                ->withErrors($val)
                ->withInput();
        }
    }

 

posted @ 2019-01-15 12:27  Sentiger  阅读(17381)  评论(0编辑  收藏  举报