为什么我的输入验证不起作用(laravel 5)?

时间:2016-04-29 04:04:34

标签: php validation laravel input laravel-5

我正在使用Laravel 5,我想对表单使用输入验证。但我的验证不起作用。你知道为什么吗?
这是观点:

<div class="form-group"><br>

        <label for="jenis_surat" class="control-label col-sm-2">Jenis Surat</label>
        <div class="col-sm-7">

          <script type="text/javascript">
        @if($errors->any())
            <div class="alert alert-danger">
                @foreach($errors->all() as $error)
                    <p>{{ $error }}</p>
                @endforeach
            </div>
        @endif
      </script>

            <input class="form-control" type="text" name="jenis_surat" id="jenis_surat">
        </div>  
        <div class="col-md-1">
          <input class="form-control" type="hidden" name="_token" value="{{ Session::token() }}">
          <a class="btn btn-success" data-toggle="modal" data-target="#konfirmasi_submit_jenis_surat">Submit</a>   
        </div>
    </div>

这是控制器:

public function tambahjenissurat(Request $request)
{
    //input validation
    $this->validate($request, [
        'jenis_surat' => 'required'
    ]);

    $jenis_surat = $request['jenis_surat'];

    $jenissurat = new JenisSurat();
    $jenissurat->jenis_surat = $jenis_surat;

    $jenissurat->save();

    return redirect()->route('jenissurat');
}

1 个答案:

答案 0 :(得分:1)

你可以这样试试吗? 1.使用use Validator;导入验证器 2.更改验证规则的代码,如下所示

$rules = array(
        'jenis_surat' => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);
    if ($validator->fails()) {
        return redirect()->back()
            ->withErrors($validator);
    }