Compare commits

...
17 Commits
Author SHA1 Message Date
Tom c55fda58b1 Merge pull request #22 from JosephSilber/patch-2
Move comment to correct spot
2015-05-06 07:23:37 -07:00
Tom 8ae4aa0627 Merge pull request #30 from barryvdh/patch-1
Add aliases to provides()
2015-05-06 07:22:33 -07:00
Tom 4edbd282ba Merge pull request #26 from toniperic/patch-1
Add support for Favicon files
2015-05-06 07:18:34 -07:00
Tom 3166c19203 Merge pull request #34 from garygreen/fix-docblocks
Fix some docblock returns
2015-05-06 07:17:50 -07:00
Gary daae1062da Fix some docblock returns 2015-04-25 13:19:56 +01:00
Tom 55eb889aca Update readme.md 2015-04-23 07:38:50 -07:00
Barry vd. Heuvel e884f50d1c Add aliases to provides() 2015-04-17 08:56:47 +02:00
Toni Perić 52e297e679 Newline at the end of the file 2015-04-07 22:06:26 +02:00
Toni Perić c3a3a44a37 Added test 2015-04-07 21:43:57 +02:00
Toni Perić 19860865d1 Update HtmlBuilder.php
Add support for Favicon files.
2015-04-07 20:45:59 +02:00
Joseph Silber 6b023d8055 Move comment to correct spot 2015-03-29 18:06:56 -04:00
Adam Engebretson 9af2030538 Merge pull request #14 from martinbean/meta
Create meta helper
2015-03-10 10:59:48 -05:00
Martin Bean 3c2e8c162f Create meta helper 2015-03-09 22:22:57 +00:00
Tom 3125f0658e Merge pull request #16 from LaravelCollective/master
Update readme.md
2015-03-07 17:08:16 -07:00
Tom 1bdfc44d8b Update readme.md 2015-03-07 17:08:05 -07:00
Robin Malfait 0d4c5f4e9d Merge pull request #13 from JosephSilber/patch-2
Use DateTime
2015-03-01 22:49:49 +01:00
Joseph Silber d07675dc0d Use DateTime 2015-03-01 16:37:25 -05:00
5 changed files with 95 additions and 274 deletions

No files matched your search

+2 -239
View File
@@ -1,246 +1,9 @@
# Forms & HTML # Forms & HTML
[![Build Status](https://travis-ci.org/LaravelCollective/html.svg)](https://travis-ci.org/LaravelCollective/html) [![Build Status](https://travis-ci.org/LaravelCollective/html.svg)](https://travis-ci.org/LaravelCollective/html)
[![Total Downloads](https://poser.pugx.org/LaravelCollective/html/downloads.svg)](https://packagist.org/packages/laravelcollective/html) [![Total Downloads](https://poser.pugx.org/LaravelCollective/html/downloads)](https://packagist.org/packages/laravelcollective/html)
[![Latest Stable Version](https://poser.pugx.org/LaravelCollective/html/v/stable.svg)](https://packagist.org/packages/laravelcollective/html) [![Latest Stable Version](https://poser.pugx.org/LaravelCollective/html/v/stable.svg)](https://packagist.org/packages/laravelcollective/html)
[![Latest Unstable Version](https://poser.pugx.org/LaravelCollective/html/v/unstable.svg)](https://packagist.org/packages/laravelcollective/html) [![Latest Unstable Version](https://poser.pugx.org/LaravelCollective/html/v/unstable.svg)](https://packagist.org/packages/laravelcollective/html)
[![License](https://poser.pugx.org/LaravelCollective/html/license.svg)](https://packagist.org/packages/laravelcollective/html) [![License](https://poser.pugx.org/LaravelCollective/html/license.svg)](https://packagist.org/packages/laravelcollective/html)
- [Installation](#installation) Official documentation for Forms & Html for The Laravel Framework can be found at the [LaravelCollective](http://laravelcollective.com) website.
- [Opening A Form](#opening-a-form)
- [CSRF Protection](#csrf-protection)
- [Form Model Binding](#form-model-binding)
- [Labels](#labels)
- [Text, Text Area, Password & Hidden Fields](#text)
- [Checkboxes and Radio Buttons](#checkboxes-and-radio-buttons)
- [File Input](#file-input)
- [Number Input](#number)
- [Drop-Down Lists](#drop-down-lists)
- [Buttons](#buttons)
- [Custom Macros](#custom-macros)
- [Generating URLs](#generating-urls)
<a name="installation"></a>
## Installation
Begin by installing this package through Composer. Edit your project's `composer.json` file to require `laravelcollective/html`.
"require": {
"laravelcollective/html": "~5.0"
}
Next, update Composer from the Terminal:
composer update
Next, add your new provider to the `providers` array of `config/app.php`:
```php
'providers' => [
// ...
'Collective\Html\HtmlServiceProvider',
// ...
],
```
Finally, add two class aliases to the `aliases` array of `config/app.php`:
```php
'aliases' => [
// ...
'Form' => 'Collective\Html\FormFacade',
'Html' => 'Collective\Html\HtmlFacade',
// ...
],
```
<a name="opening-a-form"></a>
## Opening A Form
#### Opening A Form
{!! Form::open(array('url' => 'foo/bar')) !!}
//
{!! Form::close() !!}
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'))
> **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)))
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))
<a name="csrf-protection"></a>
## CSRF Protection
#### Adding The CSRF Token To A Form
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();
#### Attaching The CSRF Filter To A Route
Route::post('profile', array('before' => 'csrf', function()
{
//
}));
<a name="form-model-binding"></a>
## Form Model Binding
#### Opening A Model Form
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)))
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!
> **Note:** When using `Form::model`, be sure to close your form with `Form::close`!
<a name="labels"></a>
## Labels
#### Generating 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.
<a name="text"></a>
## 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');
> **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());
<a name="checkboxes-and-radio-buttons"></a>
## 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);
<a name="number"></a>
## Number
#### Generating A Number Input
echo Form::number('name', 'value');
<a name="file-input"></a>
## File Input
#### Generating A File Input
echo Form::file('image');
> **Note:** The form must have been opened with the `files` option set to `true`.
<a name="drop-down-lists"></a>
## 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');
<a name="buttons"></a>
## Buttons
#### Generating 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*.
<a name="custom-macros"></a>
## Custom Macros
#### Registering A Form Macro
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();
<a name="generating-urls"></a>
##Generating URLs
For more information on generating URL's, check out the documentation on [helpers](/docs/helpers#urls).
+6 -6
View File
@@ -1,6 +1,6 @@
<?php namespace Collective\Html; <?php namespace Collective\Html;
use Carbon\Carbon; use DateTime;
use Illuminate\Routing\UrlGenerator; use Illuminate\Routing\UrlGenerator;
use Illuminate\Session\Store as Session; use Illuminate\Session\Store as Session;
use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\Macroable;
@@ -322,7 +322,7 @@ class FormBuilder {
*/ */
public function date($name, $value = null, $options = array()) public function date($name, $value = null, $options = array())
{ {
if ($value instanceof Carbon) if ($value instanceof DateTime)
{ {
$value = $value->format('Y-m-d'); $value = $value->format('Y-m-d');
} }
@@ -575,7 +575,7 @@ class FormBuilder {
* *
* @param string $value * @param string $value
* @param string $selected * @param string $selected
* @return string * @return string|null
*/ */
protected function getSelectedValue($value, $selected) protected function getSelectedValue($value, $selected)
{ {
@@ -908,7 +908,7 @@ class FormBuilder {
* *
* @param string $name * @param string $name
* @param string $value * @param string $value
* @return string * @return mixed
*/ */
public function getValueAttribute($name, $value = null) public function getValueAttribute($name, $value = null)
{ {
@@ -931,7 +931,7 @@ class FormBuilder {
* Get the model value that should be assigned to the field. * Get the model value that should be assigned to the field.
* *
* @param string $name * @param string $name
* @return string * @return mixed
*/ */
protected function getModelValueAttribute($name) protected function getModelValueAttribute($name)
{ {
@@ -949,7 +949,7 @@ class FormBuilder {
* Get a value from the session's old input. * Get a value from the session's old input.
* *
* @param string $name * @param string $name
* @return string * @return mixed
*/ */
public function old($name) public function old($name)
{ {
+39 -3
View File
@@ -97,6 +97,25 @@ class HtmlBuilder {
return '<img src="'.$this->url->asset($url, $secure).'"'.$this->attributes($attributes).'>'; return '<img src="'.$this->url->asset($url, $secure).'"'.$this->attributes($attributes).'>';
} }
/**
* Generate a link to a Favicon file.
*
* @param string $url
* @param array $attributes
* @param bool $secure
* @return string
*/
public function favicon($url, $attributes = array(), $secure = null)
{
$defaults = array('rel' => 'shortcut icon', 'type' => 'image/x-icon');
$attributes = $attributes + $defaults;
$attributes['href'] = $this->url->asset($url, $secure);
return '<link'.$this->attributes($attributes).'>'.PHP_EOL;
}
/** /**
* Generate a HTML link. * Generate a HTML link.
* *
@@ -339,9 +358,6 @@ class HtmlBuilder {
{ {
$html = array(); $html = array();
// For numeric keys we will assume that the key and the value are the same
// as this will convert HTML attributes such as "required" to a correct
// form like required="required" instead of using incorrect numerics.
foreach ((array) $attributes as $key => $value) foreach ((array) $attributes as $key => $value)
{ {
$element = $this->attributeElement($key, $value); $element = $this->attributeElement($key, $value);
@@ -361,6 +377,9 @@ class HtmlBuilder {
*/ */
protected function attributeElement($key, $value) protected function attributeElement($key, $value)
{ {
// For numeric keys we will assume that the key and the value are the same
// as this will convert HTML attributes such as "required" to a correct
// form like required="required" instead of using incorrect numerics.
if (is_numeric($key)) $key = $value; if (is_numeric($key)) $key = $value;
if ( ! is_null($value)) return $key.'="'.e($value).'"'; if ( ! is_null($value)) return $key.'="'.e($value).'"';
@@ -399,4 +418,21 @@ class HtmlBuilder {
return $safe; return $safe;
} }
/**
* Generate a meta tag.
*
* @param string $name
* @param string $content
* @param array $attributes
* @return string
*/
public function meta($name, $content, array $attributes = [])
{
$defaults = compact('name', 'content');
$attributes = array_merge($defaults, $attributes);
return '<meta'.$this->attributes($attributes).'>'.PHP_EOL;
}
} }
+1 -1
View File
@@ -61,7 +61,7 @@ class HtmlServiceProvider extends ServiceProvider {
*/ */
public function provides() public function provides()
{ {
return array('html', 'form'); return array('html', 'form', 'Collective\Html\HtmlBuilder', 'Collective\Html\FormBuilder');
} }
} }
+47 -25
View File
@@ -10,36 +10,58 @@ use Illuminate\Routing\RouteCollection;
class HtmlBuilderTest extends PHPUnit_Framework_TestCase { class HtmlBuilderTest extends PHPUnit_Framework_TestCase {
/** /**
* Setup the test environment. * Setup the test environment.
*/ */
public function setUp() public function setUp()
{ {
$this->urlGenerator = new UrlGenerator(new RouteCollection, Request::create('/foo', 'GET')); $this->urlGenerator = new UrlGenerator(new RouteCollection, Request::create('/foo', 'GET'));
$this->htmlBuilder = new HtmlBuilder($this->urlGenerator); $this->htmlBuilder = new HtmlBuilder($this->urlGenerator);
} }
/**
* Destroy the test environment.
*/
public function tearDown()
{
m::close();
}
/** public function testDl()
* Destroy the test environment. {
*/ $list = [
public function tearDown() 'foo' => 'bar',
{ 'bing' => 'baz'
m::close(); ];
}
public function testDl() $attributes = ['class' => 'example'];
{
$list = [
'foo' => 'bar',
'bing' => 'baz'
];
$attributes = ['class' => 'example']; $result = $this->htmlBuilder->dl($list, $attributes);
$result = $this->htmlBuilder->dl($list, $attributes); $this->assertEquals('<dl class="example"><dt>foo</dt><dd>bar</dd><dt>bing</dt><dd>baz</dd></dl>', $result);
}
$this->assertEquals('<dl class="example"><dt>foo</dt><dd>bar</dd><dt>bing</dt><dd>baz</dd></dl>', $result); public function testMeta()
} {
$result = $this->htmlBuilder->meta('description', 'Lorem ipsum dolor sit amet.');
$this->assertEquals('<meta name="description" content="Lorem ipsum dolor sit amet.">'.PHP_EOL, $result);
}
public function testMetaOpenGraph()
{
$result = $this->htmlBuilder->meta(null, 'website', ['property' => 'og:type']);
$this->assertEquals('<meta content="website" property="og:type">'.PHP_EOL, $result);
}
public function testFavicon()
{
$this->urlGenerator->forceRootUrl('http://foo.com');
$target = $this->urlGenerator->to('bar.ico');
$result = $this->htmlBuilder->favicon('http://foo.com/bar.ico');
$this->assertEquals('<link rel="shortcut icon" type="image/x-icon" href="'.$target.'">' . PHP_EOL, $result);
}
} }