Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

NumberToLocalizedStringTransformer allow empty on reverseTransform #37493

Copy link
Copy link
Closed
@anuri73

Description

@anuri73
Issue body actions

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

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.