09. Laravel 4 表單

表單的起始和結束標籤

<!-- app/views/form.blade.php -->
{{ Form::open(array('url' => 'our/target/route')) }}
{{ Form::close() }}

以上內容將編譯爲(自動加入 _token ):php

<form method="POST" action="http://demo.dev/our/target/route" accept-charset="UTF-8">
    <input name="_token" type="hidden" value="83KCsmJF1Z2LMZfhb17ihvt9ks5NEcAwFoRFTq6u">
</form>

起始標籤的其它用法:

自定義屬性
{{ Form::open(array(
    'url' => 'our/target/route',
    'method' => 'GET',
    'accept-charset' => 'ISO-8859-1'
)) }}
請求類型 POST GET PUT DELETE 默認爲 POST
{{ Form::open(array(
    'url' => 'our/target/route',
    'method' => 'DELETE'
)) }}
文件上傳
{{ Form::open(array(
    'url' => 'our/target/route',
    'files' => true
)) }}
提交到命名路由
{{ Form::open(array(
    'route' => 'my_route'
)) }}
提交到指定控制器方法
{{ Form::open(array(
    'action' => 'MyController@myAction'
)) }}

表單元素

Label

{{ Form::label('first_name', 'First Name', array('class' => 'f_class')) }}

以上內容將編譯爲:安全

<label for="first_name" class="f_class">First Name</label>

Text(文本框)

{{ Form::text('first_name', 'Taylor Otwell', array('id' => 'first_name')) }}

以上內容將編譯爲:app

<input name="first_name" type="text" value="Taylor Otwell" id="first_name">

若是有同名的 labelid 會自動補完,無需額外定義:post

{{ Form::label('first_name', 'First Name') }}
{{ Form::text('first_name', 'Taylor Otwell') }}

Textarea(文本域)

{{ Form::label('description', 'Description') }}
{{ Form::textarea(‘description', 'Best field ever!') }}

Password(密碼框)

{{ Form::label('secret', 'Super Secret') }}
{{ Form::password('secret') }}

Checkboxes(多選框)

{{ Form::label('pandas_are_cute', 'Are pandas cute?') }}
{{ Form::checkbox('pandas_are_cute', '1', true) }}

Radio(單選框)

{{ Form::open(array('url' => 'my/route')) }}
    {{ Form::label('panda_colour', 'Pandas are?') }}
    {{ Form::radio('panda_colour', 'red', true) }} Red
    {{ Form::radio('panda_colour', 'black') }} Black
    {{ Form::radio('panda_colour', 'white') }} White
{{ Form::close() }}

Select(下拉列表)

{{ Form::label('panda_colour', 'Pandas are?') }}
{{ Form::select('panda_colour', array(
    'red' => 'Red',
    'black' => 'Black',
    'white' => 'White'
), 'red') }}

分組下拉列表:ui

{{ Form::label('bear', 'Bears are?') }}
{{ Form::select('bear', array(
    'Panda' => array(
        'red' => 'Red',
        'black' => 'Black',
        'white' => 'White'
    ),
    'Character' => array(
        'pooh' => 'Pooh',
        'baloo' => 'Baloo'
    )
), 'black') }}

Email(電子郵箱)

{{ Form::label('email', 'E-Mail Address') }}
{{ Form::email('email', 'me@daylerees.com') }}

File Upload(文件上傳)

{{ Form::open(array(
    'url'   => 'my/route',
    'files' => true
)) }}
    {{ Form::label('avatar', 'Avatar') }}
    {{ Form::file('avatar') }}
{{ Form::close() }}

Hidden(隱藏域)

{{ Form::hidden('panda', 'luishi') }}

表單按鈕

Submit(提交按鈕)

{{ Form::submit('Save') }}

Normal Buttons(普通按鈕)

{{ Form::button('Smile') }}

Image Buttons(圖片按鈕)

{{ Form::image(asset('my/image.gif', 'submit')) }}

Reset Button(重置按鈕)

{{ Form::reset('Clear') }}

自定義宏

// app/macros.php
Form::macro('fullName', function($name)
{
    return '<p>Full name: <input type="text" name="'.$name.'"></p>';
});

調用方法:url

{{ Form::fullName('my_field') }}

表單的安全性

請使用 csrf 前置過濾器:code

// app/routes.php
Route::post('/handle-form', array('before' => 'csrf', function()
{
    // Handle our posted form data.
}));

注意: 若不使用系統提供的表單起始和結束標籤 Form::open() Form::close() 請手動添加表單 _tokenorm

<form action="{{ url('handle-form') }}" method="POST">
    {{ Form::token() }}
</form>

表單的舊數據填充

還記得第8章中提到的「舊數據的獲取方法」嗎?
使用系統提供的表單的最大的一個好處,就是系統將自動處理這些舊數據來填充表單。
完美解決了「當用戶提交錯誤,重定向到以前頁面時舊數據丟失,表單空白」的問題。而不是單純的使用JS的「回退」。csrf

相關文章
相關標籤/搜索