Readouble

Laravel 4.2 フォームとHTML

フォームの開始Opening A Form

フォームの開始Opening A Form

{{ Form::open(array('url' => 'foo/bar')) }}
	//
{{ Form::close() }}

デフォルトではPOSTメソッドが使用されます。しかし他のメソッドも指定できます。By default, a POST method will be assumed; however, you are free to specify another method:

echo Form::open(array('url' => 'foo/bar', 'method' => 'put'))

注目: 隠しフィールドをHTMLフォームがサポートしているのはPOSTGETだけなため、PUTDELETEメソッドでは_methodを自動的に付け加えることでごまかしています。Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form.

名前付きルートやコントローラーアクションを指定してフォームを開くこともできます。You may also open forms that point to named routes or controller actions:

echo Form::open(array('route' => 'route.name'))

echo Form::open(array('action' => 'Controller@method'))

ルートパラメーターを同時に渡すこともできます。You may pass in route parameters as well:

echo Form::open(array('route' => array('route.name', $user->id)))

echo Form::open(array('action' => array('Controller@method', $user->id)))

フォームでファイルのアップロードを受け付ける場合、filesオプションを配列に付け加えてください。If your form is going to accept file uploads, add a files option to your array:

echo Form::open(array('url' => 'foo/bar', 'files' => true))

CSRF対策CSRF Protection

CSRFトークンをフォームに追加するAdding The CSRF Token To A Form

Laravelはクロスサイトリクエストフォージェリからアプリケーションを保護する簡単な方法を提供しています。最初に、ランダムなトークンをセッションに設置します。Form::openメソッドをPOSTPUTDELETEで使用するとそのフォームの隠しフィールドとして自動的にCSRFトークンが追加されます。もしくは、隠しCSRFフィールドをフォーム自分で追加したい場合は、tokenメソッドを使用してください。Laravel provides an easy method of protecting your application from cross-site request forgeries. First, a random token is placed in your user's session. If you use the Form::open method with POST, PUT or DELETE the CSRF token will be added to your forms as a hidden field automatically. Alternatively, if you wish to generate the HTML for the hidden CSRF field, you may use the token method:

echo Form::token();

ルートにCSRFフィルターを設置するAttaching The CSRF Filter To A Route

Route::post('profile', array('before' => 'csrf', function()
{
	//
}));

Formとモデルの結合Form Model Binding

モデルフォームを開くOpening A Model Form

しばしばモデルの内容に基づいてフォームを取り扱いたいことがあることでしょう。これを行うにはForm::modelメソッドを使用します。Often, you will want to populate a form based on the contents of a model. To do so, use the Form::model method:

echo Form::model($user, array('route' => array('user.update', $user->id)))

これでテキスト入力のようなフォーム要素を生成すると、モデルの要素と同じ名前のフィールドに自動的に値が設定されます。ですから例えばemailという名前のテキスト入力には、ユーザーモデルのemail属性の値がセットされます。それだけではありません!セッションのフラッシュデーターとして、入力項目と同じ名前のアイテムが存在すれば、モデルの値より優先的に設定されます。そのため、優先順位は以下のようになります。Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named email, the user model's email attribute would be set as the value. However, there's more! If there is an item in the Session flash data matching the input name, that will take precedence over the model's value. So, the priority looks like this:

  1. セッションのフラッシュデーター(直前の入力)Session Flash Data (Old Input)
  2. 明示的に指定された値Explicitly Passed Value
  3. モデルの属性の値Model Attribute Data

これによりモデルの値とフォームを結びつけるだけでなく、サーバーサイドのバリデーションエラーがある場合に入力項目の再表示が簡単にできるようになります!This allows you to quickly build forms that not only bind to model values, but easily re-populate if there is a validation error on the server!

注目: Form::modelを使用する場合、Form::closeを用いてフォームを閉じてください。Note: When using Form::model, be sure to close your form with Form::close!

ラベルLabels

ラベル要素の生成Generating A Label Element

echo Form::label('email', 'E-Mail Address');

追加のHTML要素の指定Specifying Extra HTML Attributes

echo Form::label('email', 'E-Mail Address', array('class' => 'awesome'));

注目: ラベル作成後、ラベル名と同じフォーム要素を生成する場合、自動的にラベル名のIDが付きます。Note: After creating a label, any form element you create with a name matching the label name will automatically receive an ID matching the label name as well.

テキスト、テキストエリア、パスワード、隠しフィールドText, Text Area, Password & Hidden Fields

テキスト入力エリアの生成Generating A Text Input

echo Form::text('username');

デフォルト値の指定Specifying A Default Value

echo Form::text('email', 'example@gmail.com');

注目: hiddentextareaメソッドは、textメソッドと同じ使い方をします。Note: The hidden and textarea methods have the same signature as the text method.

パスワードの生成Generating A Password Input

echo Form::password('password');

その他の入力を生成するGenerating Other Inputs

echo Form::email($name, $value = null, $attributes = array());
echo Form::file($name, $attributes = array());

チェックボックスとラジオボタンCheckboxes and Radio Buttons

チェックボック、ラジオボタンを生成するGenerating A Checkbox Or Radio Input

echo Form::checkbox('name', 'value');

echo Form::radio('name', 'value');

チェック済みのチェックボックス、ラジオボタンを生成するGenerating A Checkbox Or Radio Input That Is Checked

echo Form::checkbox('name', 'value', true);

echo Form::radio('name', 'value', true);

数値Number

数値入力を生成するGenerating A Number Input

echo Form::number('name', 'value');

ファイル入力File Input

ファイル入力を生成するGenerating A File Input

echo Form::file('image');

**注意:**フォームはfilesオプションをtrueにセットし、開く必要があります。Note: The form must have been opened with the files option set to true.

ドロップダウンリストDrop-Down Lists

ドロップダウンリストを生成Generating A Drop-Down List

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

選択済みのデフォルトを指定し、ドロップダウンリストを生成Generating A Drop-Down List With Selected Default

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

グループ分けしたリストを生成するGenerating A Grouped List

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

範囲を指定して、ドロップダウンリストを生成Generating A Drop-Down List With A Range

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

月名を指定し、リストを生成Generating A List With Month Names

echo Form::selectMonth('month');

ボタンButtons

サブミットボタンを生成Generating A Submit Button

echo Form::submit('Click Me!');

注目: ボタン要素を生成する必要があるのですか?どうぞbuttonメソッドをお試しください。submitと同じ使い方です。Note: Need to create a button element? Try the button method. It has the same signature as submit.

カスタムマクロCustom Macros

フォームマクロを登録するRegistering A Form Macro

"macro"と名付けた機能により、Formクラスのヘルパを簡単に定義できます。どの様に使うのか見てください。最初に、マクロに名前とクロージャーを登録します。It's easy to define your own custom Form class helpers called "macros". Here's how it works. First, simply register the macro with a given name and a Closure:

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

次にその名前でマクロを呼び出します。Now you can call your macro using its name:

カスタムマクロフォームを呼び出すCalling A Custom Form Macro

echo Form::myField();

##URLの生成

URLの生成についてもっと知りたい場合は、ヘルパの章を参照してください。For more information on generating URL's, check out the documentation on helpers[/docs/4.2/helpers#urls].

章選択

Artisan CLI

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

テストコード表示
両コード表示
Pestのみ表示
PHPUnitのみ表示
和文変換

対象文字列と置換文字列を半角スペースで区切ってください。(最大5組各10文字まで)

本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作