Skip to content

Error during translation extraction: "Form choice is not an array" and "Choice label is not a scalar string" #179

@ovsep404

Description

@ovsep404

Description

When running the translation extractor (php bin/console translation:extract), I encounter the following errors:

  • Form choice is not an array
  • Choice label is not a scalar string

Image

These errors occur in my form types where dynamic choices are generated via function calls. For example, in my ProductType form I use:

->add('vendor', EntityType::class, [
    'class' => Vendor::class,
    'choices' => $this->vendorRepository->findBy($vendorFilters),
    'required' => false,
])

And in another form, I have:

$builder->add('state', ChoiceType::class, [
    'choices' => $this->enumChoicesWithAll(EquipmentState::class),
    'data' => AbstractRepository::FILTER_ALL_ID,
]);

Problem

The issue arises because:

  • findBy() returns an array of entities (T[]), but the translation extractor expects a static array literal (an instance of Node\Expr\Array_).
  • I think the extractor can only analyze static arrays defined directly in the code. It cannot process arrays created dynamically at runtime via method calls like findBy() or helper functions like enumChoicesWithAll().
  • When the extractor encounters these dynamic calls, it sees a Node\Expr\MethodCall or Node\Expr\FuncCall instead of a static array, which triggers the errors:
    • Form choice is not an array: The extractor cannot process the dynamically generated choices.
    • Choice label is not a scalar string: In similar dynamic cases, the extractor is unable to determine the label because it isn’t provided as a static scalar string.

The error is triggered in the following block of code in the translation extractor: FormTypeChoices.php:

} elseif (!$choices instanceof Node\Expr\Array_) {
    $this->addError($choices, 'Form choice is not an array');
    continue;
}

Here, the result of findBy() is treated as a method call (Node\Expr\MethodCall), not as a static array node, which leads to the error.

  • Form choice is not an array: The extractor cannot process the dynamically generated choices.
  • Choice label is not a scalar string: In similar dynamic cases, the extractor is unable to determine the label because it isn’t provided as a static scalar string.

Question

I am wondering if there is an alternative approach that would allow the translation extractor to handle dynamic choices without requiring the use of multiple @Ignore annotations. Has anyone encountered a similar issue or found a solution that would let the extractor process dynamically generated arrays (or provide a configurable fallback during extraction)?

Any guidance, suggestions, or workarounds would be greatly appreciated.

Thank you for your time and support!


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions