Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
Fixes #1068 by checking if translation file exists before adding it a…
Browse files Browse the repository at this point in the history
…s a resource
  • Loading branch information
Carson Full authored and fabpot committed Jan 9, 2015
1 parent f64ac7b commit 965f589
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Silex/Provider/FormServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ public function register(Application $app)

if (isset($app['translator'])) {
$r = new \ReflectionClass('Symfony\Component\Form\Form');
$app['translator']->addResource('xliff', dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf', $app['locale'], 'validators');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$app['translator']->addResource('xliff', $file, $app['locale'], 'validators');
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Silex/Provider/ValidatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public function register(Application $app)
$app['validator'] = $app->share(function ($app) {
if (isset($app['translator'])) {
$r = new \ReflectionClass('Symfony\Component\Validator\Validator');
$app['translator']->addResource('xliff', dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf', $app['locale'], 'validators');
$file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf';
if (file_exists($file)) {
$app['translator']->addResource('xliff', $file, $app['locale'], 'validators');
}
}

return new Validator(
Expand Down
24 changes: 24 additions & 0 deletions tests/Silex/Tests/Provider/FormServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
use Silex\Application;
use Silex\Provider\FormServiceProvider;
use Silex\Provider\TranslationServiceProvider;
use Silex\Provider\ValidatorServiceProvider;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
use Symfony\Component\Form\FormTypeGuesserChain;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Translation\Exception\NotFoundResourceException;

class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -112,6 +114,28 @@ public function testFormServiceProviderWillUseTranslatorIfAvailable()
$this->assertFalse($form->isValid());
$this->assertContains('ERROR: German translation', $form->getErrorsAsString());
}

public function testFormServiceProviderWillNotAddNonexistentTranslationFiles()
{
$app = new Application(array(
'locale' => 'nonexistent',
));

$app->register(new FormServiceProvider());
$app->register(new ValidatorServiceProvider());
$app->register(new TranslationServiceProvider(), array(
'locale_fallbacks' => array(),
));

$app['form.factory'];
$translator = $app['translator'];

try {
$translator->trans('test');
} catch (NotFoundResourceException $e) {
$this->fail('Form factory should not add a translation resource that does not exist');
}
}
}

class DummyFormType extends AbstractType
Expand Down
23 changes: 23 additions & 0 deletions tests/Silex/Tests/Provider/ValidatorServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
namespace Silex\Tests\Provider;

use Silex\Application;
use Silex\Provider\TranslationServiceProvider;
use Silex\Provider\ValidatorServiceProvider;
use Silex\Provider\FormServiceProvider;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Validator\Constraints as Assert;
use Silex\Tests\Provider\ValidatorServiceProviderTest\Constraint\Custom;
use Silex\Tests\Provider\ValidatorServiceProviderTest\Constraint\CustomValidator;
Expand Down Expand Up @@ -103,6 +105,27 @@ public function testValidatorConstraint($email, $isValid, $nbGlobalError, $nbEma
$this->assertEquals($nbEmailError, count($form->offsetGet('email')->getErrors()));
}

public function testValidatorWillNotAddNonexistentTranslationFiles()
{
$app = new Application(array(
'locale' => 'nonexistent',
));

$app->register(new ValidatorServiceProvider());
$app->register(new TranslationServiceProvider(), array(
'locale_fallbacks' => array(),
));

$app['validator'];
$translator = $app['translator'];

try {
$translator->trans('test');
} catch (NotFoundResourceException $e) {
$this->fail('Validator should not add a translation resource that does not exist');
}
}

public function testValidatorConstraintProvider()
{
// Email, form is valid , nb global error, nb email error
Expand Down

0 comments on commit 965f589

Please sign in to comment.