2013-07-07 399 views
7

預先填充表單字段,我們可以添加「價值」,形成create.blade.php領域:定義默認值Laravel表單字段

{{ Form::text('title', 'Some default title') }} 

有沒有辦法做到這一點任務別處(可能在模型或控制器中?)。我想在編輯視圖中創建相同的表單域代碼。謝謝!

+0

爲什麼不把表單代碼放到單獨的部分視圖中,然後將它包含在創建和編輯視圖中? – Andreyco

+0

嗨安德烈科!我打算嘗試使用if語句(如果模式爲create,使用默認值,或者在模式爲edit時使用null),則可以使用表單字段的section或variable。 我也想在其他地方設置它,例如:設置新項目的類別(與「當前」類別相同)。 – my2c

+0

您可能想要查看使用ViewModel的「模式」。 **解釋**是[這裏](http://fuelphp.com/docs/general/viewmodels。html)**注意:**閱讀這個概念,不要嘗試在你的Laravel項目中使用FuelPHP。 [這裏是](http://techportal.inviqa.com/2010/11/02/creative-mvc-meet-the-viewmodel-pattern/)Zend的一個例子。 – fideloper

回答

12

好吧,所以我們在這裏...我在示例中使用了Laravel的表單模型綁定。 (我使用用戶模型/數據庫表)。 如果這個話題並不清楚你,看看這個http://laravel.com/docs/html#form-model-binding

// Controller 

class UsersController extends BaseController 
{ 

    ... 

    // Method to show 'create' form & initialize 'blank' user's object 
    public function create() 
    { 
     $user = new User; 
     return View::make('users.form', compact('user')); 
    } 

    // This method should store data sent form form (for new user) 
    public function store() 
    { 
     print_r(Input::all()); 
    } 

    // Retrieve user's data from DB by given ID & show 'edit' form 
    public function edit($id) 
    { 
     $user = User::find($id); 
     return View::make('users.form', compact('user')); 
    } 

    // Here you should process form data for user that exists already. 
    // Modify/convert some input data maybe, save it then... 
    public function update($id) 
    { 
     $user = User::find($id); 
     print_r($user->toArray()); 
    } 

    ... 

} 

這裏來由控制器所服務的視圖文件。

// The view file - self describing I think 
<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
</head> 
<body> 
    @if(!$user->id) 
    {{ Form::model($user, ['route' => 'admin.users.store']) }} 
    @else 
    {{ Form::model($user, ['route' => ['admin.users.update', $user->id], 'method' => 'put']) }} 
    @endif 
     {{ Form::text('firstName') }} 

     {{ Form::text('lastName') }} 

     {{ Form::submit() }} 
    {{ Form::close() }} 
</body> 
</html> 
+0

單個視圖文件比我想嘗試的更好,更容易維護(兩個視圖文件和一個常見的formfields文件:));大!!!謝謝!我需要的唯一變化是if語句---> @if(!isset($ user-> id)) – my2c

0

,如果你的意思是佔位符,你可以做到這一點

{{ Form::password('password', array('placeholder' => 'password'))}} 
+0

謝謝mahami,它不是佔位符。 – my2c

5

是的,讓我們看看下面的例子:

查看:

{{ Form::text('title', $title) }} 

控制器:

$title = 'Some default title'; 
if($article) { 
    $title = $article->title; 
} 
return View::make('user.login')->with('title', $title) 

然後你會有一個文本輸入Some default title或$ article的標題,如果$article不等於false

+0

謝謝,維克多!添加$ title變量來查看,然後附加'with'部分: - > with('title','Some default ttle') 工作。如果我有相同的查看代碼進行編輯,它會抱怨丟失的標題變量,所以我補充: - > with('title',null) – my2c

+0

不知何故,當我選擇Andreyco的答案時,這被取消爲接受答案。兩者都提供了有用的答案給我的「是否有辦法」的問題:)謝謝,所有! – my2c

0

當您使用架構生成器(在遷移或其他地方):

Schema::create( 
     'posts', function($table) { 
      $table->string('title', 30)->default('New post'); 
     } 
); 
+0

您好Ivan,適用於數據庫默認值,它不會更改模型或其他模塊中的任何內容。 – my2c

1

所有你需要做的是在您的刀片模板條件。

讓我們假設您的數據庫表中有一個myfield字段,您希望將其默認爲mydefault。

就包括在這是由創建和編輯視圖稱爲局部視圖如下:

@if(isset($myfield)) 
{{ Form::input('text','myfield') }} 
@else 
{{ Form::input('text','myfield','mydefault') }} 
@endif 

你沒有別的了。

1

可能更容易(Laravel 5):

{{ Form::text('title', isset($yourModel) ? null : 'Some default title') }} 

即是,假定正在使用的形式作爲部分。如果表單的模型存在(你正在編輯或修補記錄),它應該填充值,否則它會顯示你想要的默認值。