Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
MoneyDataMapper example
$money
=
new
Money
(
20.5, 'EUR'
)
;
$factory
= Forms::
createFormFactoryBuilder
(
)
->
addExtensions
(
[
new
ValidatorExtension
(
Validation::
createValidator
(
)
)
]
)
->
getFormFactory
(
)
;
$builder
=
$factory
->
createBuilder
(
FormType::
class
,
$money
,
[
'invalid_message' => 'not the one to display'
]
)
->
add
(
'amount', TextType::
class
)
->
add
(
'currency', CurrencyType::
class
)
;
$builder
->
setDataMapper
(
new
MoneyDataMapper
(
)
)
;
$form
=
$builder
->
getForm
(
)
;
$form
->
submit
(
[
'amount' => 'invalid_amount', 'currency' => 'USD'
]
)
;
$this
->
assertFalse
(
$form
->
isValid
(
)
)
;
$this
->
assertNull
(
$form
->
getData
(
)
)
;
$this
->
assertCount
(
1,
$form
->
getErrors
(
)
)
;
$this
->
assertSame
(
'Expected numeric value',
$form
->
getTransformationFailure
(
)
->
getMessage
(
)
)
;
$error
=
$form
->
getErrors
(
)
[
0
]
;
$this
->
assertSame
(
'Money amount should be numeric. "invalid_amount" is invalid.',
$error
->
getMessage
(
)
)
;
}