Skip to content

[Form] MoneyType: allow "string" as input #59710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jmsche opened this issue Feb 6, 2025 · 3 comments · Fixed by #59993
Closed

[Form] MoneyType: allow "string" as input #59710

jmsche opened this issue Feb 6, 2025 · 3 comments · Fixed by #59993

Comments

@jmsche
Copy link
Contributor

jmsche commented Feb 6, 2025

Description

I use the MoneyType with the scale option set to 4.

The property is a string, using the decimal Doctrine type. And the setter on the entity accepts a string as argument.

When submitting the form & saving the entity into the database, there's always an update of the entity, even if the value has not changed (it's rounding e.g. 0.0000 to 0).

I noticed the issue disappears if I use the same form configuration, but using the NumberType instead, with the input option set to string. However, the input option of the MoneyType does not accept the string value.

I currently fixed the issue with a form extension:

<?php

declare(strict_types=1);

namespace App\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\DataTransformer\StringToFloatTransformer;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormBuilderInterface;

final class MoneyTypeExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->addModelTransformer(new StringToFloatTransformer($options['scale']));
    }

    public static function getExtendedTypes(): iterable
    {
        return [MoneyType::class];
    }
}

But I think it would be better to allow string for the input option like the NumberType does?

Example

No response

@carsonbot carsonbot added the Form label Feb 6, 2025
@xabbuh xabbuh added the Feature label Feb 6, 2025
@StevenRenaux
Copy link
Contributor

Hi @jmsche, maybe I miss understand something. I created a reproducer on sqlite but I don't find the same issue.
https://github.com/StevenRenaux/symfony-issue-59710

When I'm submitting the same value (e.g.151.0000) or remove a zero behind the decimal point:

  • With NumberType => No more than one query even if we remove a few zeros after the decimal point
    Image

  • With NumberType and input string => Updated every time even when we remove a zero behind the decimal point
    Image

  • With MoneyType => No more than one query even if we remove a few zeros after the decimal point
    Image

I think the issue come from somewhere else

@jmsche
Copy link
Contributor Author

jmsche commented Mar 4, 2025

Hi Steven,

Indeed with sqlite it seems to behave okay.

However when using MariaDB with your reproducer I'm able to reproduce :/

@jmsche
Copy link
Contributor Author

jmsche commented Mar 4, 2025

After mounting a Postgresql database, I can confirm the bug also happens with Postgres 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants