Description
Symfony version(s) affected: 5.1.2
Symfony/Form: v5.1.2
Description
When input on the browser is disabled browser will not send any data at all to the server. At the same time if input is NumberType, symfony will user NumberToLocalizedStringTransformer to normalize data. If we look at NumberToLocalizedStringTransformer::reverseTransform
, there is type check:
if (!\is_string($value)) {
throw new TransformationFailedException('Expected a string.');
}
So, when input is disabled on the frontend this check will throw an exception. I think it shouldn't do. Null or missing value validation is responsibility of NotBlank validator.
How to reproduce
->add('projectsPublishedLimit', NumberType::class, [
'block_prefix' => 'service_level_number',
'empty_data' => null,
'html5' => true,
'label_format' => 'form.label.%name%',
'required' => false,
'scale' => 0,
'translation_domain' => self::TRANSLATION_DOMAIN,
])
disable it on the frontend. Could be done via settings disabled="disabled" or via js:
document.getElementById('projectsPublishedLimit').disabled = $status;
Possible Solution
Maybe I am using in a wrong way, but what if allow null or missing value in this transformation?
Something like this:
public function reverseTransform($value) {
if (empty($value)) {
return null;
}
if (!\is_string($value)) {
throw new TransformationFailedException('Expected a string.');
}
# ....
return $this->round($result);
}
Additional context