Reputation: 2562
Symfony2 automatically translate the input field values with type decimal or integer. I have a two languages for my app: arabic and english I created an Entity with the following field:
/**
* @var float $price
*
* @ORM\Column(name="price", type="decimal", scale=2, nullable=true)
*
* @Assert\Regex(pattern="/^[0-9]+(\.\d{1,2})?$/",message="Incorrect price.")
* @Assert\Type(type="float")
* @Assert\Min(0)
*/
private $price;
In form I let the sf to guess a field type:
$builder->add('price')
I load the form for editing this entity in Arabic Interface.
In the price field I see ١٢٫٤
instead of 12.40
.
I can't save the form because HTML5 validation is failed.
If I enter 12.40 in the current field and save Entity, 12 will be saved, instead of 12.40.
Why? How to disable it? how to validate the Arabic digits? Any suggestions?
EDIT: solved, see below
Upvotes: 4
Views: 1794
Reputation: 301
This is an old post, but i hope my answer will help anyone encounters this issue. Instead of disabling intl extension as suggested above, you can use Data Transformers in form type to manually set the default locale to English and that will let the fields to be shown with English digits.
1) Use Data Transformers to interrupt (hook into) the process of showing the field data.
2) Manually set Locale to English.
here is what i've done with dateOfBirth field in the form type:
$builder -> add(
$builder -> create(
'dateOfBirth', DateType::class, array(
'label' => 'Date of Birth',
'widget' => 'single_text',
'required' => false,
'html5' => false,
'attr' => ['class' => 'form-control']
)) -> addModelTransformer(new CallbackTransformer(
function($dateOfBirth) {
\Locale::setDefault('en');
return $dateOfBirth;
},
function($dateOfBirth) {
return $dateOfBirth;
}
))
);
Upvotes: 0
Reputation: 2562
I found the answer why it happens here As you can see, symfony register a ViewTransformer for these widget types:
$builder->addViewTransformer(
new IntegerToLocalizedStringTransformer(
$options['precision'],
$options['grouping'],
$options['rounding_mode']
));
Current transformer transform an integer value to localized string. It happens for the number
widget (NumberToLocalizedStringTransformer) and money
widget (MoneyToLocalizedStringTransformer) too.
So I think need to register a new FieldType which will not used a ViewTransformer.
EDIT: I solved the problem just disabling intl extension and now all numeric fields are using a default english numbers. If you enable the intl extension you should use only localized numbers in the input values, it's a default behavior.
Upvotes: 2