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 284f419 commit cb711fe
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
14 changes: 7 additions & 7 deletions examples/html5.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@
$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', array(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', array(0, 100));

$form->addText('email', 'Send to email:')
->setType('email')
->setAttribute('autocomplete', 'off')
->setAttribute('placeholder', 'Optional, but Recommended')
->setHtmlType('email')
->setHtmlAttribute('autocomplete', 'off')
->setHtmlAttribute('placeholder', 'Optional, but Recommended');
->addCondition($form::FILLED) // conditional rule: if is email filled, ...
->addRule($form::EMAIL, 'Incorrect email address'); // ... then check email

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)
{
$control = new Controls\TextInput($label, $maxLength);
$control->setAttribute('size', $cols);
$control->setHtmlAttribute('size', $cols);
return $this[$name] = $control;
}

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


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

Expand Down Expand Up @@ -364,7 +364,7 @@ public function addSelect($name, $label = NULL, array $items = NULL, $size = NUL
{
$control = new Controls\SelectBox($label, $items);
if ($size > 1) {
$control->setAttribute('size', (int) $size);
$control->setHtmlAttribute('size', (int) $size);
}
return $this[$name] = $control;
}
Expand All @@ -382,7 +382,7 @@ public function addMultiSelect($name, $label = NULL, array $items = NULL, $size
{
$control = new Controls\MultiSelectBox($label, $items);
if ($size > 1) {
$control->setAttribute('size', (int) $size);
$control->setHtmlAttribute('size', (int) $size);
}
return $this[$name] = $control;
}
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 @@ -317,6 +317,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('Nette\Utils\Html', $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('Nette\Utils\Html', $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 (array('text', 'search', 'tel', 'url', 'email') as $type) {
$input->setType($type);
$input->setHtmlType($type);
Assert::same('<input type="' . $type . '" name="text" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\' pattern="[0-9]+">', (string) $input->getControl());
}
$input->setType('password');
$input->setHtmlType('password');
Assert::same('<input type="password" name="text" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\' pattern="[0-9]+">', (string) $input->getControl());

$input->setType('number');
$input->setHtmlType('number');
Assert::same('<input type="number" name="text" 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', array(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 cb711fe

Please sign in to comment.