是否有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')