Skip to content

[Form][BC break] Decimal fields cannot be type-hinted as float #32124

Closed
@yceruto

Description

@yceruto

Symfony version(s) affected: 4.3

Description
I have the follow entity and field definition:

class Product
{
    /**
     * @ORM\Column(type="decimal", precision=10, scale=2)
     */
    private $price = 0;

    public function getPrice(): float
    {
        return $this->price;
    }

    public function setPrice(float $price)
    {
        $this->price = $price;
    }
}

and a form field whose "type" is being guessed by DoctrineOrmTypeGuesser:

$builder->add('price');

this works well before 4.3, but after upgrade this app, when the form is being built, this error appears:

Unable to transform data for property path "price": Expected a numeric string.

Possible Solution
It will depend on the right way of type-hinting decimal fields.

Even if #30893 helps to solve a real problem with Doctrine's change detection, I think we must fix the BC break, either relaxing the transformation constraint allowing numeric values:

if (!\is_string($value) || !is_numeric($value)) {
throw new TransformationFailedException('Expected a numeric string.');
}

-if (!\is_string($value) || !is_numeric($value)) {
-    throw new TransformationFailedException('Expected a numeric string.');
+if (!\is_numeric($value)) {
+    throw new TransformationFailedException('Expected a number or a numeric string.');
}

or reverting the change in DoctrineOrmTypeGuesser:

case Type::DECIMAL:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', ['input' => 'string'], Guess::MEDIUM_CONFIDENCE);

deprecating this default, for later change in 5.0 to input = string.

WDYT?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions