Skip to content

Commit e62c412

Browse files
authored
Merge pull request #129 from yiisoft/enctype
Add shortcut methods for enctype in `Form`
2 parents 842f5ac + 98da681 commit e62c412

File tree

3 files changed

+102
-34
lines changed

3 files changed

+102
-34
lines changed

CHANGELOG.md

+35-34
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,83 @@
22

33
## 2.4.1 under development
44

5-
- no changes in this release.
5+
- New #129: Add methods `enctypeApplicationXWwwFormUrlencoded()`, `enctypeMultipartFormData()` and `enctypeTextPlain()`
6+
to `Form` tag class (@vjik)
67

78
## 2.4.0 May 19, 2022
89

910
- New #97: Add classes for tags `Body`, `Article`, `Section`, `Nav`, `Aside`, `Hgroup`, `Header`, `Footer`, `Address`
1011
and methods `Html::body()`, `Html::article()`, `Html::section()`, `Html::nav()`, `Html::aside()`, `Html::hgroup()`,
11-
`Html::header()`, `Html::footer()`, `Html::address()` (soodssr)
12-
- New #103: Add class for tag `Form` and method `Html::form()` (vjik)
12+
`Html::header()`, `Html::footer()`, `Html::address()` (@soodssr)
13+
- New #103: Add class for tag `Form` and method `Html::form()` (@vjik)
1314
- New #105: Add specialized class `File` for an input tag with type `file` and methods `Html::file()` and
14-
`Input::fileControl()` (vjik)
15-
- New #109: Add class for tag `Datalist` and method `Html::datalist()` (vjik)
15+
`Input::fileControl()` (@vjik)
16+
- New #109: Add class for tag `Datalist` and method `Html::datalist()` (@vjik)
1617
- New #109, #117: Add specialized class for input tag with type `Range` and methods `Html::range()`,
17-
`Input::range()` (vjik)
18-
- New #111: Add widget `ButtonGroup` (vjik)
19-
- New #111: Add method `Tag::unionAttributes()` that available for all tags (vjik)
20-
- New #113: Add class for tag `Legend`, class for tag `Fieldset`, methods `Html::legend()` and `Html::fieldset()` (vjik)
21-
- Enh #102: Remove psalm type `HtmlAttributes`, too obsessive for package users (vjik)
18+
`Input::range()` (@vjik)
19+
- New #111: Add widget `ButtonGroup` (@vjik)
20+
- New #111: Add method `Tag::unionAttributes()` that available for all tags (@vjik)
21+
- New #113: Add class for tag `Legend`, class for tag `Fieldset`, methods `Html::legend()` and `Html::fieldset()` (@vjik)
22+
- Enh #102: Remove psalm type `HtmlAttributes`, too obsessive for package users (@vjik)
2223
- Enh #104: Add parameter `$attributes` to methods `Html::input()`, `Html::buttonInput()`, `Html::submitInput()`
23-
and `Html::resetInput()` (vjik)
24-
- Enh #106: Add option groups support to method `Select::optionsData()` (vjik)
25-
- Enh #108: Add individual attributes of options and option groups support to method `Select::optionsData()` (vjik)
26-
- Enh #115: Add methods `CheckboxList::name()` and `RadioList::name()` (vjik)
24+
and `Html::resetInput()` (@vjik)
25+
- Enh #106: Add option groups support to method `Select::optionsData()` (@vjik)
26+
- Enh #108: Add individual attributes of options and option groups support to method `Select::optionsData()` (@vjik)
27+
- Enh #115: Add methods `CheckboxList::name()` and `RadioList::name()` (@vjik)
2728

2829
## 2.3.0 March 25, 2022
2930

30-
- New #95: Add class for tag `Title` and method `Html::title()` (vjik)
31+
- New #95: Add class for tag `Title` and method `Html::title()` (@vjik)
3132
- New #96: Add classes for heading tags `H1-6` and methods `Html::h1()`, `Html::h2()`, `Html::h3()`, `Html::h4()`,
32-
`Html::h5()`, `Html::h6()` (vjik)
33-
- New #100: Add classes for tags `Picture`, `Audio`, `Video`, `Source` and `Track` (Gerych1984, vjik)
33+
`Html::h5()`, `Html::h6()` (@vjik)
34+
- New #100: Add classes for tags `Picture`, `Audio`, `Video`, `Source` and `Track` (@Gerych1984, @vjik)
3435

3536
## 2.2.1 October 24, 2021
3637

37-
- Enh #93: Add support for `yiisoft/arrays` version `^2.0` (vjik)
38+
- Enh #93: Add support for `yiisoft/arrays` version `^2.0` (@vjik)
3839

3940
## 2.2.0 October 20, 2021
4041

41-
- New #89: Add method `nofollow()` to the `A` tag (soodssr)
42+
- New #89: Add method `nofollow()` to the `A` tag (@soodssr)
4243
- New #90: Add method `itemsFromValues()` to widgets `RadioList` and `CheckboxList` that set items with labels equal
43-
to values (vjik)
44+
to values (@vjik)
4445
- New #92: A third optional argument `$attributes` containing tag attributes in terms of name-value pairs has been
4546
added to methods `Html::textInput()`, `Html::hiddenInput()`, `Html::passwordInput()`, `Html::fileInput()`,
46-
`Html::radio()`, `Html::checkbox()`, `Html::textarea()` (vjik)
47+
`Html::radio()`, `Html::checkbox()`, `Html::textarea()` (@vjik)
4748

4849
## 2.1.0 September 23, 2021
4950

5051
- New #88: Add `Noscript` tag support and shortcuts for `Script` tag via methods `Script::noscript()`
51-
and `Script::noscriptTag()` (vjik)
52+
and `Script::noscriptTag()` (@vjik)
5253

5354
## 2.0.0 August 24, 2021
5455

55-
- New #74: Add classes for tags `Em`, `Strong`, `B` and `I` (vjik)
56-
- New #75: Add methods `as()` and `preload()` to the `Link` tag (vjik)
57-
- New #76: Add `NoEncode` class designed to wrap content that should not be encoded in HTML tags (vjik)
56+
- New #74: Add classes for tags `Em`, `Strong`, `B` and `I` (@vjik)
57+
- New #75: Add methods `as()` and `preload()` to the `Link` tag (@vjik)
58+
- New #76: Add `NoEncode` class designed to wrap content that should not be encoded in HTML tags (@vjik)
5859
- New #78: Allow pass `null` argument to methods `Tag::class()`, `Tag::replaceClass()`, `BooleanInputTag::label()` and
59-
`BooleanInputTag::sideLabel()` (vjik)
60+
`BooleanInputTag::sideLabel()` (@vjik)
6061
- New #82: Add support individual attributes for inputs in `CheckboxList` and `RadioList` widgets via methods
6162
`CheckboxList::individualInputAttributes()`, `CheckboxList::replaceIndividualInputAttributes()`,
62-
`RadioList::individualInputAttributes()` and `RadioList::replaceIndividualInputAttributes()` (vjik)
63-
- Chg #79: Do not add empty attribute value for empty strings (vjik)
64-
- Bug #83: Fix `Html::ATTRIBUTE_ORDER` values (terabytesoftw)
63+
`RadioList::individualInputAttributes()` and `RadioList::replaceIndividualInputAttributes()` (@vjik)
64+
- Chg #79: Do not add empty attribute value for empty strings (@vjik)
65+
- Bug #83: Fix `Html::ATTRIBUTE_ORDER` values (@terabytesoftw)
6566

6667
## 1.2.0 May 04, 2021
6768

6869
- New #70: Add support `\Stringable` as content in methods `Html::tag()`, `Html::normalTag()`, `Html::a()`,
6970
`Html::label()`, `Html::option()`, `Html::div()`, `Html::span()`, `Html::p()`, `Html::li()`, `Html::caption()`,
70-
`Html::td()`, `Html::th()` (vjik)
71-
- New #71: Add methods `Script::getContent()` and `Style::getContent()` (vjik)
71+
`Html::td()`, `Html::th()` (@vjik)
72+
- New #71: Add methods `Script::getContent()` and `Style::getContent()` (@vjik)
7273

7374
## 1.1.0 April 09, 2021
7475

75-
- New #65: Add classes for table tags `Table`, `Caption`, `Colgroup`, `Col`, `Thead`, `Tbody`, `Tfoot`, `Tr`, `Th`, `Td` (vjik)
76-
- New #69: Add class for tag `Br` (vjik)
76+
- New #65: Add classes for table tags `Table`, `Caption`, `Colgroup`, `Col`, `Thead`, `Tbody`, `Tfoot`, `Tr`, `Th`, `Td` (@vjik)
77+
- New #69: Add class for tag `Br` (@vjik)
7778

7879
## 1.0.1 April 04, 2021
7980

80-
- Bug #68: Fix `TagContentTrait::content()` and `TagContentTrait::addContent()` when used with named parameters (vjik)
81+
- Bug #68: Fix `TagContentTrait::content()` and `TagContentTrait::addContent()` when used with named parameters (@vjik)
8182

8283
## 1.0.0 March 17, 2021
8384

src/Tag/Form.php

+34
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ final class Form extends NormalTag
1515
{
1616
use TagContentTrait;
1717

18+
public const ENCTYPE_APPLICATION_X_WWW_FORM_URLENCODED = 'application/x-www-form-urlencoded';
19+
public const ENCTYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
20+
public const ENCTYPE_TEXT_PLAIN = 'text/plain';
21+
1822
private ?string $csrfToken = null;
1923
private ?string $csrfName = null;
2024

@@ -97,6 +101,36 @@ public function enctype(?string $enctype): self
97101
return $new;
98102
}
99103

104+
/**
105+
* All characters are encoded before sending.
106+
*
107+
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#form-submission-algorithm:attr-fs-enctype-urlencoded
108+
*/
109+
public function enctypeApplicationXWwwFormUrlencoded(): self
110+
{
111+
return $this->enctype(self::ENCTYPE_APPLICATION_X_WWW_FORM_URLENCODED);
112+
}
113+
114+
/**
115+
* The type that allows file `<input>` element(s) to upload file data.
116+
*
117+
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#form-submission-algorithm:attr-fs-enctype-formdata
118+
*/
119+
public function enctypeMultipartFormData(): self
120+
{
121+
return $this->enctype(self::ENCTYPE_MULTIPART_FORM_DATA);
122+
}
123+
124+
/**
125+
* Sends data without any encoding at all. Not recommended.
126+
*
127+
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#form-submission-algorithm:attr-fs-enctype-text
128+
*/
129+
public function enctypeTextPlain(): self
130+
{
131+
return $this->enctype(self::ENCTYPE_TEXT_PLAIN);
132+
}
133+
100134
/**
101135
* The method content attribute specifies how the form-data should be submitted.
102136
*

tests/common/Tag/FormTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,36 @@ public function testEnctype(string $expected, ?string $enctype): void
205205
);
206206
}
207207

208+
public function testEnctypeApplicationXWwwFormUrlencoded(): void
209+
{
210+
$this->assertSame(
211+
'<form enctype="application/x-www-form-urlencoded"></form>',
212+
Form::tag()
213+
->enctypeApplicationXWwwFormUrlencoded()
214+
->render()
215+
);
216+
}
217+
218+
public function testEnctypeMultipartFormData(): void
219+
{
220+
$this->assertSame(
221+
'<form enctype="multipart/form-data"></form>',
222+
Form::tag()
223+
->enctypeMultipartFormData()
224+
->render()
225+
);
226+
}
227+
228+
public function testEnctypeTextPlain(): void
229+
{
230+
$this->assertSame(
231+
'<form enctype="text/plain"></form>',
232+
Form::tag()
233+
->enctypeTextPlain()
234+
->render()
235+
);
236+
}
237+
208238
public function dataMethod(): array
209239
{
210240
return [
@@ -288,6 +318,9 @@ public function testImmutability(): void
288318
$this->assertNotSame($tag, $tag->action(null));
289319
$this->assertNotSame($tag, $tag->autocomplete());
290320
$this->assertNotSame($tag, $tag->enctype(null));
321+
$this->assertNotSame($tag, $tag->enctypeApplicationXWwwFormUrlencoded());
322+
$this->assertNotSame($tag, $tag->enctypeMultipartFormData());
323+
$this->assertNotSame($tag, $tag->enctypeTextPlain());
291324
$this->assertNotSame($tag, $tag->method(null));
292325
$this->assertNotSame($tag, $tag->noValidate());
293326
$this->assertNotSame($tag, $tag->target(null));

0 commit comments

Comments
 (0)