验证后填充的字段将为空 [英] filled fields are going empty after validation
本文介绍了验证后填充的字段将为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个带有验证的laravel表单,这里的问题是,当我留空字段时,它会显示错误消息(没关系),但是如果我填充了某个字段,而没有填写其他字段,则会刷新,然后刷新该字段现在填充的内容为空,并且也显示该字段的错误消息.
I have created an laravel form with validation, the problem here is that when I leave empty fields it displays the error message (that's ok) but then if I fill some field it and others not it makes a refresh and then the field that was filled now is empty and an error message for that field is displayed too.
public function store() {
$data = [
"errors" => null
];
$rules = array(
"car" => "required|unique:insur_docs,car_id",
"ownership_cert" => "required",
"authoriz" => "required",
"drive_permis" => "required",
"sgs" => "required",
"tpl" => "required",
"kasko" => "required",
"inter_permis" => "required",
);
$validation = Validator::make(Input::all(), $rules);
$cars = DB::table('cars')->orderBy('Description', 'asc')->distinct()->lists('Description', 'id');
if ($validation->passes()) {
$doc = new InsurDoc();
$doc->ownership_cert = Input::get('ownership_cert');
$doc->authoriz = Input::get('authoriz');
$doc->drive_permis = Input::get('drive_permis');
$doc->sgs = Input::get('sgs');
$doc->tpl = Input::get('tpl');
$doc->kasko = Input::get('kasko');
$doc->inter_permis = Input::get('inter_permis');
$doc->car_id = Input::get('car');
$doc->save();
return Redirect::to('/');
} else {
$data['errors'] = $validation->errors();
$data['cars'] = $cars;
return View::make('pages.insur_docs_create', $data);
}
}
视图
@if($errors->count() > 0)
<div class="alert alert-danger">
<p>The following errors have occurred:</p>
<ul>
@foreach($errors->all() as $message)
<li>{{ Helpers\Helper::macro($message) }}</li>
@endforeach
</ul>
</div>
@endif
宏功能
public static function macro($errors) {
return "<span class=\"glyphicon glyphicon-warning-sign\">"
. "</span> $errors";
}
那里出了什么问题?
推荐答案
在您的控制器中添加Input::flash()
,并在您的视图中将其与Input::old('field_name')
一起使用.
Add Input::flash()
in your controller and use it in your view with Input::old('field_name')
.
Input::flash(); // Add this
$data['errors'] = $validation->errors();
$data['cars'] = $cars;
return View::make('pages.insur_docs_create', $data);
}
}
这篇关于验证后填充的字段将为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文