Skip to content

Commit

Permalink
BaseControl::setAttribute() and setType() aliased to setHtmlAttribute…
Browse files Browse the repository at this point in the history
…() and setHtmlType()
  • Loading branch information
dg committed Dec 20, 2016
1 parent d764b90 commit b99385a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
12 changes: 6 additions & 6 deletions examples/html5.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@
$form->addGroup();

$form->addText('query', 'Search:')
->setType('search')
->setAttribute('autofocus');
->setHtmlType('search')
->setHtmlAttribute('autofocus');

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

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

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

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

Expand Down
12 changes: 6 additions & 6 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function getForm($need = TRUE)
public function addText($name, $label = NULL, $cols = NULL, $maxLength = NULL)
{
return $this[$name] = (new Controls\TextInput($label, $maxLength))
->setAttribute('size', $cols);
->setHtmlAttribute('size', $cols);
}


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


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


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


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


Expand Down
12 changes: 12 additions & 0 deletions src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,18 @@ public function getHtmlId()
* @param mixed value
* @return static
*/
public function setHtmlAttribute($name, $value = TRUE)
{
return $this->setAttribute($name, $value);
}


/**
* Alias for setHtmlAttribute()
* @param string name
* @param mixed value
* @return static
*/
public function setAttribute($name, $value = TRUE)
{
$this->control->$name = $value;
Expand Down
11 changes: 11 additions & 0 deletions src/Forms/Controls/TextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public function loadHttpData()
* @param string
* @return static
*/
public function setHtmlType($type)
{
return $this->setType($type);
}


/**
* Alias for setHtmlType()
* @param string
* @return static
*/
public function setType($type)
{
$this->control->type = $type;
Expand Down
4 changes: 2 additions & 2 deletions tests/Forms/Controls.TextArea.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test(function () {
$form = new Form;
$input = $form->addTextArea('text', 'Label')
->setValue('&text')
->setAttribute('autocomplete', 'off');
->setHtmlAttribute('autocomplete', 'off');

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

Expand Down
16 changes: 8 additions & 8 deletions tests/Forms/Controls.TextInput.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test(function () {
$form = new Form;
$input = $form->addText('text', 'Label')
->setValue('text')
->setAttribute('autocomplete', 'off');
->setHtmlAttribute('autocomplete', 'off');

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

foreach (['text', 'search', 'tel', 'url', 'email'] as $type) {
$input->setType($type);
$input->setHtmlType($type);
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());
}
$input->setType('password');
$input->setHtmlType('password');
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());

$input->setType('number');
$input->setHtmlType('number');
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());
});

Expand Down Expand Up @@ -125,7 +125,7 @@ test(function () { // validation rule MAX_LENGTH
});


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


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

0 comments on commit b99385a

Please sign in to comment.