Skip to content

Commit

Permalink
Merge pull request #52 from fprochazka/fix/objects-as-captions
Browse files Browse the repository at this point in the history
BaseControl: allow passing objects to translator in control
  • Loading branch information
dg committed Nov 29, 2014
2 parents 49c84ac + 8453bf0 commit ddb75f5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function translate($value, $count = NULL)
$tmp = is_array($value) ? array(& $value) : array(array(& $value));
foreach ($tmp[0] as & $v) {
if ($v != NULL && !$v instanceof Html) { // intentionally ==
$v = $translator->translate((string) $v, $count);
$v = $translator->translate($v, $count);
}
}
}
Expand Down
95 changes: 95 additions & 0 deletions tests/Forms/Controls.translate().phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/**
* Test: Nette\Forms translating controls with translatable strings wrapped in objects
*/

use Nette\Forms\Form,
Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class Translator implements \Nette\Localization\ITranslator
{
function translate($message, $count = NULL)
{
return is_object($message) ? get_class($message) : $message;
}
}

class StringWrapper
{
public $message;

public function __construct($message)
{
$this->message = $message;
}

public function __toString()
{
return (string) $this->message;
}
}



test(function() {
$form = new Form;
$form->setTranslator(new Translator);

$name = $form->addText('name', 'Your name');
Assert::match('<label for="frm-name">Your name</label>', (string) $name->getLabel());

$name2 = $form->addText('name2', new StringWrapper('Your name'));
Assert::match('<label for="frm-name2">StringWrapper</label>', (string) $name2->getLabel());
});



test(function() {
$form = new Form;
$form->setTranslator(new Translator);

$name = $form->addRadioList('name', 'Your name');
Assert::match('<label>Your name</label>', (string) $name->getLabel());

$name2 = $form->addRadioList('name2', new StringWrapper('Your name'));
Assert::match('<label>StringWrapper</label>', (string) $name2->getLabel());
});



test(function() {
$form = new Form;
$form->setTranslator(new Translator);

$name = $form->addText('name', 'Your name');
$name->addError("Error message");
$name->addError($w = new StringWrapper('Your name'));
Assert::same(array(
'Error message',
$w
), $name->getErrors());
});



test(function() {
$form = new Form;
$form->setTranslator(new Translator);

$email = $form->addText('email')
->addRule($form::EMAIL, 'error');
Assert::match('<input type="text" name="email" id="frm-email" data-nette-rules=\'[{"op":":email","msg":"error"}]\'>', (string) $email->getControl());
$email->validate();
Assert::same(array('error'), $email->getErrors());

$email2 = $form->addText('email2')
->addRule($form::EMAIL, new StringWrapper('Your name'));
Assert::match('<input type="text" name="email2" id="frm-email2" data-nette-rules=\'[{"op":":email","msg":"StringWrapper"}]\'>', (string) $email2->getControl());
$email2->validate();
Assert::same(array('StringWrapper'), $email2->getErrors());
});

0 comments on commit ddb75f5

Please sign in to comment.