Skip to content

Commit b99385a

Browse files
committed
BaseControl::setAttribute() and setType() aliased to setHtmlAttribute() and setHtmlType()
1 parent d764b90 commit b99385a

File tree

6 files changed

+45
-22
lines changed

6 files changed

+45
-22
lines changed

examples/html5.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@
2121
$form->addGroup();
2222

2323
$form->addText('query', 'Search:')
24-
->setType('search')
25-
->setAttribute('autofocus');
24+
->setHtmlType('search')
25+
->setHtmlAttribute('autofocus');
2626

2727
$form->addText('count', 'Number of results:')
28-
->setType('number')
28+
->setHtmlType('number')
2929
->setDefaultValue(10)
3030
->addRule($form::INTEGER, 'Must be numeric value')
3131
->addRule($form::RANGE, 'Must be in range from %d to %d', [1, 100]);
3232

3333
$form->addText('precision', 'Precision:')
34-
->setType('range')
34+
->setHtmlType('range')
3535
->setDefaultValue(50)
3636
->addRule($form::INTEGER, 'Precision must be numeric value')
3737
->addRule($form::RANGE, 'Precision must be in range from %d to %d', [0, 100]);
3838

3939
$form->addEmail('email', 'Send to email:')
40-
->setAttribute('autocomplete', 'off')
41-
->setAttribute('placeholder', 'Optional, but Recommended');
40+
->setHtmlAttribute('autocomplete', 'off')
41+
->setHtmlAttribute('placeholder', 'Optional, but Recommended');
4242

4343
$form->addSubmit('submit', 'Send');
4444

src/Forms/Container.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function getForm($need = TRUE)
241241
public function addText($name, $label = NULL, $cols = NULL, $maxLength = NULL)
242242
{
243243
return $this[$name] = (new Controls\TextInput($label, $maxLength))
244-
->setAttribute('size', $cols);
244+
->setHtmlAttribute('size', $cols);
245245
}
246246

247247

@@ -256,8 +256,8 @@ public function addText($name, $label = NULL, $cols = NULL, $maxLength = NULL)
256256
public function addPassword($name, $label = NULL, $cols = NULL, $maxLength = NULL)
257257
{
258258
return $this[$name] = (new Controls\TextInput($label, $maxLength))
259-
->setAttribute('size', $cols)
260-
->setType('password');
259+
->setHtmlAttribute('size', $cols)
260+
->setHtmlType('password');
261261
}
262262

263263

@@ -272,7 +272,7 @@ public function addPassword($name, $label = NULL, $cols = NULL, $maxLength = NUL
272272
public function addTextArea($name, $label = NULL, $cols = NULL, $rows = NULL)
273273
{
274274
return $this[$name] = (new Controls\TextArea($label))
275-
->setAttribute('cols', $cols)->setAttribute('rows', $rows);
275+
->setHtmlAttribute('cols', $cols)->setHtmlAttribute('rows', $rows);
276276
}
277277

278278

@@ -389,7 +389,7 @@ public function addCheckboxList($name, $label = NULL, array $items = NULL)
389389
public function addSelect($name, $label = NULL, array $items = NULL, $size = NULL)
390390
{
391391
return $this[$name] = (new Controls\SelectBox($label, $items))
392-
->setAttribute('size', $size > 1 ? (int) $size : NULL);
392+
->setHtmlAttribute('size', $size > 1 ? (int) $size : NULL);
393393
}
394394

395395

@@ -404,7 +404,7 @@ public function addSelect($name, $label = NULL, array $items = NULL, $size = NUL
404404
public function addMultiSelect($name, $label = NULL, array $items = NULL, $size = NULL)
405405
{
406406
return $this[$name] = (new Controls\MultiSelectBox($label, $items))
407-
->setAttribute('size', $size > 1 ? (int) $size : NULL);
407+
->setHtmlAttribute('size', $size > 1 ? (int) $size : NULL);
408408
}
409409

410410

src/Forms/Controls/BaseControl.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,18 @@ public function getHtmlId()
344344
* @param mixed value
345345
* @return static
346346
*/
347+
public function setHtmlAttribute($name, $value = TRUE)
348+
{
349+
return $this->setAttribute($name, $value);
350+
}
351+
352+
353+
/**
354+
* Alias for setHtmlAttribute()
355+
* @param string name
356+
* @param mixed value
357+
* @return static
358+
*/
347359
public function setAttribute($name, $value = TRUE)
348360
{
349361
$this->control->$name = $value;

src/Forms/Controls/TextInput.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public function loadHttpData()
4444
* @param string
4545
* @return static
4646
*/
47+
public function setHtmlType($type)
48+
{
49+
return $this->setType($type);
50+
}
51+
52+
53+
/**
54+
* Alias for setHtmlType()
55+
* @param string
56+
* @return static
57+
*/
4758
public function setType($type)
4859
{
4960
$this->control->type = $type;

tests/Forms/Controls.TextArea.render.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test(function () {
2525
$form = new Form;
2626
$input = $form->addTextArea('text', 'Label')
2727
->setValue('&text')
28-
->setAttribute('autocomplete', 'off');
28+
->setHtmlAttribute('autocomplete', 'off');
2929

3030
Assert::type(Html::class, $input->getLabel());
3131
Assert::same('<label for="frm-text">Label</label>', (string) $input->getLabel());
@@ -39,7 +39,7 @@ test(function () {
3939
test(function () { // translator
4040
$form = new Form;
4141
$input = $form->addTextArea('text', 'Label')
42-
->setAttribute('placeholder', 'place')
42+
->setHtmlAttribute('placeholder', 'place')
4343
->setValue('text')
4444
->setTranslator(new Translator);
4545

tests/Forms/Controls.TextInput.render.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test(function () {
2525
$form = new Form;
2626
$input = $form->addText('text', 'Label')
2727
->setValue('text')
28-
->setAttribute('autocomplete', 'off');
28+
->setHtmlAttribute('autocomplete', 'off');
2929

3030
Assert::type(Html::class, $input->getLabel());
3131
Assert::same('<label for="frm-text">Label</label>', (string) $input->getLabel());
@@ -39,7 +39,7 @@ test(function () {
3939
test(function () { // translator
4040
$form = new Form;
4141
$input = $form->addText('text', 'Label')
42-
->setAttribute('placeholder', 'place')
42+
->setHtmlAttribute('placeholder', 'place')
4343
->setValue('text')
4444
->setTranslator(new Translator)
4545
->setEmptyValue('xxx');
@@ -76,13 +76,13 @@ test(function () { // validation rule required & PATTERN
7676
->addRule($form::PATTERN, 'error message', '[0-9]+');
7777

7878
foreach (['text', 'search', 'tel', 'url', 'email'] as $type) {
79-
$input->setType($type);
79+
$input->setHtmlType($type);
8080
Assert::same('<input type="' . $type . '" name="text" pattern="[0-9]+" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\'>', (string) $input->getControl());
8181
}
82-
$input->setType('password');
82+
$input->setHtmlType('password');
8383
Assert::same('<input type="password" name="text" pattern="[0-9]+" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\'>', (string) $input->getControl());
8484

85-
$input->setType('number');
85+
$input->setHtmlType('number');
8686
Assert::same('<input type="number" name="text" pattern="[0-9]+" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\'>', (string) $input->getControl());
8787
});
8888

@@ -125,7 +125,7 @@ test(function () { // validation rule MAX_LENGTH
125125
});
126126

127127

128-
test(function () { // validation rule RANGE without setType
128+
test(function () { // validation rule RANGE without setHtmlType
129129
$form = new Form;
130130
$minInput = $form->addText('min');
131131
$maxInput = $form->addText('max');
@@ -139,12 +139,12 @@ test(function () { // validation rule RANGE without setType
139139
});
140140

141141

142-
test(function () { // validation rule RANGE with setType
142+
test(function () { // validation rule RANGE with setHtmlType
143143
$form = new Form;
144144
$minInput = $form->addText('min');
145145
$maxInput = $form->addText('max');
146146
$input = $form->addText('count')
147-
->setType('number')
147+
->setHtmlType('number')
148148
->addRule(Form::RANGE, 'Must be in range from %d to %d', [0, 100])
149149
->addRule(Form::MIN, 'Must be greater than or equal to %d', 1)
150150
->addRule(Form::MAX, 'Must be less than or equal to %d', 101)

0 commit comments

Comments
 (0)