我正在使用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');
}
答案 0 :(得分:1)
use Validator;
导入验证器
2.更改验证规则的代码,如下所示
$rules = array(
'jenis_surat' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator);
}