-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from fprochazka/fix/objects-as-captions
BaseControl: allow passing objects to translator in control
- Loading branch information
Showing
2 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); |