Compare commits

...
20 Commits
Author SHA1 Message Date
Tom Shafer 04c596a699 Update composer.json 2018-03-23 17:39:21 -07:00
Tom Shafer 3b177c17aa Update composer.json 2018-03-23 17:38:57 -07:00
Tortue Torche 33984f5e87 [5.5] Fix form method of hidden input '_method' is preserved on next request (#505)
This fix was merged in the pull request https://github.com/LaravelCollective/html/pull/460
But the pull request https://github.com/LaravelCollective/html/pull/471 accidentally revert this fix
2018-03-22 07:00:50 -07:00
Anderson Friaça b79edc7acf [5.5] Validation in select field for boolean value of selected option (#490)
* [5.5] Validation in select field for boolean value of selected option

* Adjustment
2018-02-12 06:19:55 -08:00
Tom Shafer 215f20b8c2 Update composer.json 2018-02-08 08:25:51 -08:00
Luiz Gustavo Martins 2c5db92f66 Fix some inputs treated like nestedModel when using an accessor (#471)
* 5.6 Prep

* Added $optgroupsAttributes parameter to select method.

* escape HTML entities in `src`-attribute

* Style fixes. Replaced == with ===.
2018-02-05 07:04:02 -07:00
Tom Shafer 7e49960e8f Merge pull request #421 from thetar/patch-1
Adds getModel()
2018-02-05 06:45:48 -07:00
Tom Shafer f3b55c6033 Merge pull request #442 from izniburak/5.5-array_merge
+ operator doesn't merge. Replaced by array_merge method.
2018-02-05 06:43:39 -07:00
Tom Shafer e2fcbee917 Merge pull request #460 from tortuetorche/5.5
[5.5] Form method of hidden input '_method' is preserved on next request
2018-02-04 15:03:46 -07:00
Tom Shafer ac46825a97 Merge pull request #453 from mvaraujo/patch-1
#452
2018-02-04 14:41:29 -07:00
Tom Shafer 33dd7bb48e Merge pull request #448 from peterparkers/patch-1
Update composer.json
2018-02-04 14:38:05 -07:00
Tom Shafer 779d075f79 Merge pull request #458 from nickurt/patch-1
Remove PHP_EOL statements by HTML-tags ..
2018-02-04 14:37:47 -07:00
nickurt 29253c13a3 fixed tests .. 2018-01-03 14:50:42 +01:00
nickurt d621ce1357 remove php_eol statements by html-tags #456 2018-01-03 14:25:43 +01:00
Tortue Torche 9e2a40555a Merge branch 'patch-1' into 5.5 2017-12-22 14:09:39 +01:00
Marcus Vinicius Araujo 8f7bc4d097 #452
This shall be occuring for check-boxes. Please, check'em for us.
2017-12-18 17:23:46 -02:00
Phurinat Puekkham cf13e067eb Update composer.json
more secure
2017-12-04 19:40:08 +07:00
İzni Burak Demirtaş b39f510897 + operator doesn't merge. Replaced by array_merge method. 2017-10-26 10:37:42 +03:00
Tony e6bc7e5fea Adds getModel()
It would be nice to be able to get the model before setting a new one (maybe in a partial) and having the original model to set back at the end of the partial.
2017-09-14 10:51:14 -05:00
Tortue Torche 3688b0e433 Form method of hidden input '_method' is preserved on next request
E.G. When you submitted data across a form with a 'PATCH' method and then in a second submit you use a form with a 'DELETE' method, the FormBuilder::getValueAttribute() method use the previous value of the hidden input '_method'.
So the value is replaced by 'PATCH' instead of the 'DELETE' value.
2017-08-09 14:23:33 +02:00
6 changed files with 115 additions and 48 deletions

No files matched your search

+4 -1
View File
@@ -2,7 +2,7 @@
"name": "laravelcollective/html",
"description": "HTML and Form Builders for the Laravel Framework",
"license": "MIT",
"homepage": "http://laravelcollective.com",
"homepage": "https://laravelcollective.com",
"support": {
"issues": "https://github.com/LaravelCollective/html/issues",
"source": "https://github.com/LaravelCollective/html"
@@ -39,6 +39,9 @@
]
},
"extra": {
"branch-alias": {
"dev-master": "5.5-dev"
},
"laravel": {
"providers": [
"Collective\\Html\\HtmlServiceProvider"
+1 -1
View File
@@ -87,7 +87,7 @@ trait FormAccessible
$mutator = collect($methods)
->first(function (ReflectionMethod $method) use ($key) {
return $method->getName() == 'form' . Str::studly($key) . 'Attribute';
return $method->getName() === 'form' . Str::studly($key) . 'Attribute';
});
return (bool) $mutator;
+52 -28
View File
@@ -190,6 +190,16 @@ class FormBuilder
{
$this->model = $model;
}
/**
* Get the current model instance on the form builder.
*
* @return mixed $model
*/
public function getModel()
{
return $this->model;
}
/**
* Close the current form.
@@ -562,9 +572,10 @@ class FormBuilder
*
* @param string $name
* @param array $list
* @param string $selected
* @param string|bool $selected
* @param array $selectAttributes
* @param array $optionsAttributes
* @param array $optgroupsAttributes
*
* @return \Illuminate\Support\HtmlString
*/
@@ -573,7 +584,8 @@ class FormBuilder
$list = [],
$selected = null,
array $selectAttributes = [],
array $optionsAttributes = []
array $optionsAttributes = [],
array $optgroupsAttributes = []
) {
$this->type = 'select';
@@ -599,8 +611,9 @@ class FormBuilder
}
foreach ($list as $value => $display) {
$optionAttributes = isset($optionsAttributes[$value]) ? $optionsAttributes[$value] : [];
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes);
$optionAttributes = $optionsAttributes[$value] ?? [];
$optgroupAttributes = $optgroupsAttributes[$value] ?? [];
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes);
}
// Once we have all of this HTML, we can join this into a single element after
@@ -675,13 +688,14 @@ class FormBuilder
* @param string $value
* @param string $selected
* @param array $attributes
* @param array $optgroupAttributes
*
* @return \Illuminate\Support\HtmlString
*/
public function getSelectOption($display, $value, $selected, array $attributes = [])
public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = [])
{
if (is_array($display)) {
return $this->optionGroup($display, $value, $selected, $attributes);
return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes);
}
return $this->option($display, $value, $selected, $attributes);
@@ -694,18 +708,21 @@ class FormBuilder
* @param string $label
* @param string $selected
* @param array $attributes
* @param array $optionsAttributes
*
* @return \Illuminate\Support\HtmlString
*/
protected function optionGroup($list, $label, $selected, array $attributes = [])
protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = [])
{
$html = [];
foreach ($list as $value => $display) {
$html[] = $this->option($display, $value, $selected, $attributes);
}
$optionAttributes = $optionsAttributes[$value] ?? [];
return $this->toHtmlString('<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>');
$html[] = $this->option($display, $value, $selected, $optionAttributes);
}
return $this->toHtmlString('<optgroup label="' . e($label) . '"' . $this->html->attributes($attributes) . '>' . implode('', $html) . '</optgroup>');
}
/**
@@ -722,9 +739,14 @@ class FormBuilder
{
$selected = $this->getSelectedValue($value, $selected);
$options = ['value' => $value, 'selected' => $selected] + $attributes;
$options = array_merge(['value' => $value, 'selected' => $selected], $attributes);
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>');
$string = '<option' . $this->html->attributes($options) . '>';
if ($display !== null) {
$string .= e($display) . '</option>';
}
return $this->toHtmlString($string);
}
/**
@@ -762,8 +784,10 @@ class FormBuilder
} elseif ($selected instanceof Collection) {
return $selected->contains($value) ? 'selected' : null;
}
return ((string) $value == (string) $selected) ? 'selected' : null;
if (is_int($value) && is_bool($selected)) {
return (bool)$value === $selected;
}
return ((string) $value === (string) $selected) ? 'selected' : null;
}
/**
@@ -844,7 +868,7 @@ class FormBuilder
return $this->getRadioCheckedState($name, $value, $checked);
default:
return $this->getValueAttribute($name) == $value;
return $this->getValueAttribute($name) === $value;
}
}
@@ -865,7 +889,7 @@ class FormBuilder
return false;
}
if ($this->missingOldAndModel($name) && !$request) {
if ($this->missingOldAndModel($name) && is_null($request)) {
return $checked;
}
@@ -897,7 +921,7 @@ class FormBuilder
return $checked;
}
return $this->getValueAttribute($name) == $value;
return $this->getValueAttribute($name) === $value;
}
/**
@@ -996,7 +1020,7 @@ class FormBuilder
{
$method = strtoupper($method);
return $method != 'GET' ? 'POST' : $method;
return $method !== 'GET' ? 'POST' : $method;
}
/**
@@ -1098,7 +1122,7 @@ class FormBuilder
// If the method is something other than GET we will go ahead and attach the
// CSRF token to the form, as this can't hurt and is convenient to simply
// always have available on every form the developers creates for them.
if ($method != 'GET') {
if ($method !== 'GET') {
$appendage .= $this->token();
}
@@ -1140,7 +1164,7 @@ class FormBuilder
$old = $this->old($name);
if (! is_null($old) && $name != '_method') {
if (! is_null($old) && $name !== '_method') {
return $old;
}
@@ -1151,7 +1175,7 @@ class FormBuilder
if ($hasNullMiddleware
&& is_null($old)
&& is_null($value)
&& !is_null($this->view->shared('errors'))
&& ! is_null($this->view->shared('errors'))
&& count($this->view->shared('errors')) > 0
) {
return null;
@@ -1159,7 +1183,7 @@ class FormBuilder
}
$request = $this->request($name);
if (!is_null($request)) {
if (! is_null($request) && $name !== '_method') {
return $request;
}
@@ -1179,7 +1203,7 @@ class FormBuilder
*/
protected function request($name)
{
if (!isset($this->request)) {
if (! isset($this->request)) {
return null;
}
@@ -1217,16 +1241,16 @@ class FormBuilder
$key = $this->transformKey($name);
$payload = $this->session->getOldInput($key);
if (!is_array($payload)) {
if (! is_array($payload)) {
return $payload;
}
if (!in_array($this->type, ['select', 'checkbox'])) {
if (!isset($this->payload[$key])) {
if (! in_array($this->type, ['select', 'checkbox'])) {
if (! isset($this->payload[$key])) {
$this->payload[$key] = collect($payload);
}
if (!empty($this->payload[$key])) {
if (! empty($this->payload[$key])) {
$value = $this->payload[$key]->shift();
return $value;
}
@@ -1243,7 +1267,7 @@ class FormBuilder
*/
public function oldInputIsEmpty()
{
return (isset($this->session) && count($this->session->getOldInput()) == 0);
return (isset($this->session) && count($this->session->getOldInput()) === 0);
}
/**
+11 -11
View File
@@ -78,7 +78,7 @@ class HtmlBuilder
{
$attributes['src'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<script' . $this->attributes($attributes) . '></script>' . PHP_EOL);
return $this->toHtmlString('<script' . $this->attributes($attributes) . '></script>');
}
/**
@@ -94,11 +94,11 @@ class HtmlBuilder
{
$defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet'];
$attributes = $attributes + $defaults;
$attributes = array_merge($attributes, $defaults);
$attributes['href'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>' . PHP_EOL);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>');
}
/**
@@ -132,11 +132,11 @@ class HtmlBuilder
{
$defaults = ['rel' => 'shortcut icon', 'type' => 'image/x-icon'];
$attributes = $attributes + $defaults;
$attributes = array_merge($attributes, $defaults);
$attributes['href'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>' . PHP_EOL);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>');
}
/**
@@ -162,7 +162,7 @@ class HtmlBuilder
$title = $this->entities($title);
}
return $this->toHtmlString('<a href="' . $url . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
return $this->toHtmlString('<a href="' . $this->entities($url) . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
}
/**
@@ -357,7 +357,7 @@ class HtmlBuilder
{
$html = '';
if (count($list) == 0) {
if (count($list) === 0) {
return $html;
}
@@ -451,7 +451,7 @@ class HtmlBuilder
}
// Treat boolean attributes as HTML properties
if (is_bool($value) && $key != 'value') {
if (is_bool($value) && $key !== 'value') {
return $value ? $key : '';
}
@@ -511,7 +511,7 @@ class HtmlBuilder
$attributes = array_merge($defaults, $attributes);
return $this->toHtmlString('<meta' . $this->attributes($attributes) . '>' . PHP_EOL);
return $this->toHtmlString('<meta' . $this->attributes($attributes) . '>');
}
/**
@@ -525,8 +525,8 @@ class HtmlBuilder
*/
public function tag($tag, $content, array $attributes = [])
{
$content = is_array($content) ? implode(PHP_EOL, $content) : $content;
return $this->toHtmlString('<' . $tag . $this->attributes($attributes) . '>' . PHP_EOL . $this->toHtmlString($content) . PHP_EOL . '</' . $tag . '>' . PHP_EOL);
$content = is_array($content) ? implode('', $content) : $content;
return $this->toHtmlString('<' . $tag . $this->attributes($attributes) . '>' . $this->toHtmlString($content) . '</' . $tag . '>');
}
/**
+37
View File
@@ -402,6 +402,37 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
'<select class="class-name" id="select-id" name="size"><optgroup label="Large sizes"><option value="L">Large</option><option value="XL">Extra Large</option></optgroup><option value="S">Small</option></select>'
);
$select = $this->formBuilder->select(
'size',
[
'Large sizes' => [
'L' => 'Large',
'XL' => 'Extra Large',
],
'M' => 'Medium',
'Small sizes' => [
'S' => 'Small',
'XS' => 'Extra Small',
],
],
null,
[],
[
'Large sizes' => [
'L' => ['disabled']
],
'M' => ['disabled'],
],
[
'Small sizes' => ['disabled'],
]
);
$this->assertEquals(
$select,
'<select name="size"><optgroup label="Large sizes"><option value="L" disabled>Large</option><option value="XL">Extra Large</option></optgroup><option value="M" disabled>Medium</option><optgroup label="Small sizes" disabled><option value="S">Small</option><option value="XS">Extra Small</option></optgroup></select>'
);
$select = $this->formBuilder->select(
'encoded_html',
['no_break_space' => '&nbsp;', 'ampersand' => '&amp;', 'lower_than' => '&lt;'],
@@ -433,6 +464,12 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
'<select name="countries"><option value="1" selected="selected">L</option><option value="2">M</option></select>',
$result
);
$select = $this->formBuilder->select('avc', [1 => 'Yes', 0 => 'No'], true, ['placeholder' => 'Select']);
$this->assertEquals(
'<select name="avc"><option value="">Select</option><option value="1" selected>Yes</option><option value="0" >No</option></select>',
$select
);
}
public function testSelectCollection()
+10 -7
View File
@@ -65,7 +65,7 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
{
$result = $this->htmlBuilder->meta('description', 'Lorem ipsum dolor sit amet.');
$this->assertEquals('<meta name="description" content="Lorem ipsum dolor sit amet.">' . PHP_EOL, $result);
$this->assertEquals('<meta name="description" content="Lorem ipsum dolor sit amet.">', $result);
}
public function testTag()
@@ -83,17 +83,17 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
$result4 = $this->htmlBuilder->tag('div', $content, ['class' => 'row']);
$this->assertEquals('<p>' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '</p>' . PHP_EOL, $result1);
$this->assertEquals('<p class="text-center">' . PHP_EOL . 'Lorem ipsum dolor sit amet.' . PHP_EOL . '</p>' . PHP_EOL, $result2);
$this->assertEquals('<div class="row">' . PHP_EOL . '<p>Lorem ipsum dolor sit amet.</p>' . PHP_EOL . '</div>' . PHP_EOL, $result3);
$this->assertEquals('<div class="row">' . PHP_EOL . '<img src="http://example.com/image1">' . PHP_EOL . '<img src="http://example.com/image2">' . PHP_EOL . '</div>' . PHP_EOL, $result4);
$this->assertEquals('<p>Lorem ipsum dolor sit amet.</p>', $result1);
$this->assertEquals('<p class="text-center">Lorem ipsum dolor sit amet.</p>', $result2);
$this->assertEquals('<div class="row"><p>Lorem ipsum dolor sit amet.</p></div>', $result3);
$this->assertEquals('<div class="row"><img src="http://example.com/image1"><img src="http://example.com/image2"></div>', $result4);
}
public function testMetaOpenGraph()
{
$result = $this->htmlBuilder->meta(null, 'website', ['property' => 'og:type']);
$this->assertEquals('<meta content="website" property="og:type">' . PHP_EOL, $result);
$this->assertEquals('<meta content="website" property="og:type">', $result);
}
public function testFavicon()
@@ -102,7 +102,7 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
$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);
$this->assertEquals('<link rel="shortcut icon" type="image/x-icon" href="' . $target . '">', $result);
}
public function testComponentRegistration()
@@ -118,8 +118,11 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
$result2 = $this->htmlBuilder->link("http://www.example.com", "<span>Example.com</span>", ["class" => "example-link"], null, false);
$result3 = $this->htmlBuilder->link("https://a.com/b?id=4&not_id=5", "URL which needs escaping");
$this->assertEquals('<a href="http://www.example.com" class="example-link">&lt;span&gt;Example.com&lt;/span&gt;</a>', $result1);
$this->assertEquals('<a href="http://www.example.com" class="example-link"><span>Example.com</span></a>', $result2);
$this->assertEquals('<a href="https://a.com/b?id=4&amp;not_id=5">URL which needs escaping</a>', $result3);
}
public function testMailto()