是否有Laravel任何实用功能,让你给一个替代值特定输入字段,如果旧值是空的?目前我有以下代码:输入Laravel替代值,如果输入::老空
<input type="text" class="form-control" id="title" name="title" value="{{ (!empty(Input::old('title'))) ? Input::old('title') : 'hey' }}">
但它并不真的那么漂亮。有任何想法吗?
是否有Laravel任何实用功能,让你给一个替代值特定输入字段,如果旧值是空的?目前我有以下代码:输入Laravel替代值,如果输入::老空
<input type="text" class="form-control" id="title" name="title" value="{{ (!empty(Input::old('title'))) ? Input::old('title') : 'hey' }}">
但它并不真的那么漂亮。有任何想法吗?
是的!不要使用input
标签:)
如果您使用{{ Form
您将得到这个,以及更多!
{{ Form::text('email', null, array('class'=>'form-control', 'placeholder'=>'Email Address')) }}
退房这里的文档(http://laravel.com/docs/html & http://laravel.com/docs/requests),你会发现,当输入被刷新到会话,通过改变叶片呈现此输入框中会自动替换“空”(第二个参数)与会议中闪现的价值。
这样就省去了检查旧的输入或有任何讨厌的if/else检查你的模板中。此外,您不再需要担心任何HTML代码注入或XSS发生,因为Form :: text将确保文本正确转换为其HTML实体。
在检查错误的地方,应该使用Laravel验证器。一些与此类似:
protected function createUser(){
$rules = array(
'email'=>'required|email',
'password'=>'required|min:6|confirmed',
'password_confirmation'=>'required'
);
$validator = Validator::make(Input::all(), $rules);
if (! $validator->passes()) {
Input::flashExcept('password', 'password_confirmation');
return Redirect::to('my_form');
} else {
// do stuff with the form, it's all good
}
return Redirect::intended('/complete');
}
此外,在你的模板,您可以显示所有从表单中的错误:
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
或者只是选择的第一个错误,并显示下{{ Form::text
@if ($errors->has('first_name'))
<span class="error">{{$errors->first('first_name')}}</span>
@endif
Laravel已经这一切建立在,你会得到它是免费的!使用请求,验证器,叶片/ HTML
使用
Input::old('title', 'fallback value')