Laravel 中的 Form class 可以協助產生前端表單

基本表單使用

<!-- 預設 method 是 POST -->
{{ Form::open(array('url' => 'foo/bar', 'method' => 'put')) }}

<!-- 直接指定到 action -->
{{ Form::open(array('action' => 'UsersController@index')) }}

<!-- enable files -->
{{ Form::open(array('url' => 'foo/bar', 'files' => true)) }}

<!-- Form 結束標籤 -->
{{ Form::close() }}

表單內容操作

<div>
    {{ Form::open(array('url' => 'foo/bar')) }}
        <!-- Label -->
        {{ Form::label('name', 'Name:', array('class' => 'address')) }}
        <!-- Text -->
        {{ Form::text('name', 'Johnson') }}

        <!-- Password -->
        {{ Form::password('password') }}

        <!-- Submit -->
        {{ Form::submit('Send this form!') }}
    {{ Form::close() }}
</div>

結合 Model 使用
Controller

class MemberController extends BaseController {
    public function getIndex()
    {
        $member = Member::find('johnsonlu');
        return View::make('home', array('member' => $member));
    }
}

View

<!-- binding model -->
{{ Form::model($member, array('url' => array('home/account', $member->nacid))) }}
    {{ Form::label('nacid', 'Account:', array('class' => 'address')) }}
    {{ Form::text('nacid') }}

    {{ Form::label('name', 'Name:', array('class' => 'address')) }}
    {{ Form::text('name') }}

    {{ Form::submit('Send this form!') }}
{{ Form::close() }}

Drop-Down Lists

// Default Select(name, array(value => option_str))
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'));

// Default Select
echo Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S');

// Range
echo Form::selectRange('number', 10, 20);

// Select Month
echo Form::selectMonth('month');

// Group
echo Form::select('animal', array(
    'Cats' => array('leopard' => 'Leopard'),
    'Dogs' => array('spaniel' => 'Spaniel'),
));

Custom Macros

Form::macro('myInput', function()
{
    return '<input type="awesome">';
});

echo Form::myInput();
Categories: Laravel