参考:http:///docs/5.1/html参考:http://v3.go/docs/views/html.html》》安装Begin by installing this package through Composer. Edit your project's
Next, update Composer from the Terminal:
Next, add your new provider to the
Finally, add two class aliases to the
》》打开formOpening A Form
{!! Form::open(array('url' => 'foo/bar')) !!}
//
{!! Form::close() !!} By default, a
You may also open forms that point to named routes or controller actions:
You may pass in route parameters as well:
If your form is going to accept file uploads, add a
》》CSRF ProtectionAdding The CSRF Token To A FormLaravel 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
Attaching The CSRF Filter To A Route
Route::post('profile', array('before' => 'csrf', function()
{
//
})); 》》Form Model BindingOpening A Model FormOften, you will want to populate a form based on the contents of a model. To do so, use the
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
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!
LabelsGenerating A Label Element
echo Form::label('email', 'E-Mail Address'); Specifying Extra HTML Attributes
echo Form::label('email', 'E-Mail Address', array('class' => 'awesome')); 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 FieldsGenerating A Text Input
echo Form::text('username'); Specifying A Default Value
echo Form::text('email', 'example@gmail.com'); Note: The hidden and textarea methods have the same signature as the text method. Generating A Password Input
echo Form::password('password', array('class' => 'awesome')); Generating Other Inputs
echo Form::email($name, $value = null, $attributes = array());
echo Form::file($name, $attributes = array()); Checkboxes and Radio ButtonsGenerating 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); NumberGenerating A Number Input
echo Form::number('name', 'value'); DateGenerating A Date Input
echo Form::date('name', \Carbon\Carbon::now()); File InputGenerating A File Input
echo Form::file('image'); Note: The form must have been opened with the Drop-Down ListsGenerating 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 Drop-Down List With an Empty PlaceholderThis will create an
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'); ButtonsGenerating A Submit Button
echo Form::submit('Click Me!'); Note: Need to create a button element? Try the button method. It has the same signature as submit. Custom MacrosRegistering A Form MacroIt'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:
Now you can call your macro using its name: Calling A Custom Form Macro
echo Form::myField(); Generating URLslink_toGenerate a HTML link to the given URL.
link_to_assetGenerate a HTML link to the given asset.
link_to_routeGenerate a HTML link to the given named route.
link_to_actionGenerate a HTML link to the given controller action.
|
|
来自: 阿亮love > 《laravel5》