[英]Laravel 4 - using php with laravel code blocks to get selected state
[英]Laravel update form - get correct selected state
在我的更新配置文件表單中,我有一個標題選擇列表。 誠然,這些項目應該從數據庫派生,但是目前,我希望這個特定的選擇列表能夠執行以下操作。
在初始頁面加載時顯示所選項目。 因此,如果$ user ['title']是'Mrs',則會顯示出來。
如果laravel驗證在其他表單字段上出現錯誤,並且用戶將標題更改為“ Mr”,則所選狀態應在“ Mr”項上。
到目前為止,我有以下內容,但無法正常工作。
<select name="title" id="title">
<option value="Mr" @if(($user['title']) == 'Mr') selected @else @if((Input::old('title')) == 'Mr') selected @endif @endif>Mr</option>
<option value="Mrs" @if(($user['title']) == 'Mrs') selected @else @if((Input::old('title')) == 'Mrs') selected @endif @endif>Mrs</option>
....
</select>
您應該從Controller
傳遞選擇數據,並使用Form
組件在View
構建選擇,例如:
在Controller
方法中:
$users = User::lists('title', 'id');
return View::make('user')->with('users', $user);
在View
:
// By default, item with id 1 (the first item) will be selected
echo Form::select('title', $users, Input::old('title', 1), ['id'=>title]);
如果您使用的是Blade
則:
// By default, item with id 1 (the first item) will be selected
{{ Form::select('title', $users, Input::old('title', 1), ['id'=>title]) }}
正確閱讀文檔 。 您正在將Laravel
與老式的PHP
編碼一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.