Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
965f5896
Commit
965f5896
authored
Jan 06, 2015
by
Carson Full
Committed by
Fabien Potencier
Jan 09, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1068 by checking if translation file exists before adding it as a resource
parent
f64ac7b8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
2 deletions
+55
-2
src/Silex/Provider/FormServiceProvider.php
src/Silex/Provider/FormServiceProvider.php
+4
-1
src/Silex/Provider/ValidatorServiceProvider.php
src/Silex/Provider/ValidatorServiceProvider.php
+4
-1
tests/Silex/Tests/Provider/FormServiceProviderTest.php
tests/Silex/Tests/Provider/FormServiceProviderTest.php
+24
-0
tests/Silex/Tests/Provider/ValidatorServiceProviderTest.php
tests/Silex/Tests/Provider/ValidatorServiceProviderTest.php
+23
-0
No files found.
src/Silex/Provider/FormServiceProvider.php
View file @
965f5896
...
...
@@ -78,7 +78,10 @@ class FormServiceProvider implements ServiceProviderInterface
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'
);
}
}
}
...
...
src/Silex/Provider/ValidatorServiceProvider.php
View file @
965f5896
...
...
@@ -31,7 +31,10 @@ class ValidatorServiceProvider implements ServiceProviderInterface
$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
(
...
...
tests/Silex/Tests/Provider/FormServiceProviderTest.php
View file @
965f5896
...
...
@@ -14,12 +14,14 @@ namespace Silex\Tests\Provider;
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
{
...
...
@@ -112,6 +114,28 @@ class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
$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
...
...
tests/Silex/Tests/Provider/ValidatorServiceProviderTest.php
View file @
965f5896
...
...
@@ -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
;
...
...
@@ -103,6 +105,27 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
$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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment