如何在控制器的store()中使用$ this = validate()验证laravel中的表单

时间:2017-04-08 06:14:04

标签: php laravel

我写了一段代码,但我猜它不起作用。

给定的代码写在控制器中:

public function store( Request $request )
{   
    $this -> validate( $request,
                       [ 'name' => 'required' ,
                         'status' => 'required',
                         'created_at' => 'required',
                         'created_by' => 'required',
                         'modified_at' => 'required',
                         'updated_by' => 'required' ] ); 

    if ( request( 'status' ) === 'on' )
    {
        $status = 1;
        # code...
    }
    else
    {
        $status = 0;
    }

    CategoryDetail::Create( [ 'name' => request( 'name' ),
                              'status' => $status,
                              'created_at' => request( 'created_at' ),
                              'created_by' => request( 'created_by' ),
                              'updated_at' => request( 'modified_at' ),
                              'updated_by' => request( 'modified_by' ) ] );
    \Session::flash( 'create',
                     'inserted successfully' );

    return redirect( 'category/list' ); 
}

没有异常错误。我只是想知道当字段留空或空时如何显示消息。

这是我的create.blade.php

@extends( 'adminlte::page' )
@section( 'content' )
<!DOCTYPE html>

<html>
    <head>
        <title>Create</title>
    </head>

    <body>
        <div class = "box box-info">
            <form class = "box-body form-group"
                  method = "POST" action="/category/store">
                Name:<input type = "text"
                            class = "form-control"
                            name = "name"><br>
                Status:<input type = "checkbox"
                              name = "status"><br>
                Created at:<input type = "date"
                                  name = "created_at"><br>
                Created by:<input type = "number"
                                  class = "form-control"
                                  name = "created_by"><br>
                Modified at:<input type = "date"
                                   name = "modified_at"><br>
                Modified by:<input type = "number"
                                   class = "form-control"
                                   name = "modified_by"><br>
                <input type = "submit"
                       value = "create"
                       name = "Create"><br>
                    {{csrf_field()}}
                    {{method_field('PUT')}}
            </form>
        </div>
    </body>
</html>
@endsection

1 个答案:

答案 0 :(得分:1)

你可以通过以下方式获得错误按摩:

@if (count($errors) > 0)
                        <div class="alert alert-danger alert-dismissable mb30 rtl yekan">
                            <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                            <ul class="list">
                                @foreach($errors->all() as $err)
                                    <li class="liste-item">{{ $err }}</li>
                                @endforeach
                            </ul>
                        </div>
@endif