簡體   English   中英

Laravel更新表單-獲取正確的選定狀態

[英]Laravel update form - get correct selected state

在我的更新配置文件表單中,我有一個標題選擇列表。 誠然,這些項目應該從數據庫派生,但是目前,我希望這個特定的選擇列表能夠執行以下操作。

  1. 在初始頁面加載時顯示所選項目。 因此,如果$ user ['title']是'Mrs',則會顯示出來。

  2. 如果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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM