FormExtension::isSelectedChoice()

bool isSelectedChoice(ChoiceView $choice, string|array $selectedValue)

Returns whether a choice is selected for a given form value.

Unfortunately Twig does not support an efficient way to execute the "is_selected" closure passed to the template by ChoiceType. It is faster to implement the logic here (around 65ms for a specific form).

Directly implementing the logic here is also faster than doing so in ChoiceView (around 30ms).

The worst option tested so far is to implement the logic in ChoiceView and access the ChoiceView method directly in the template. Doing so is around 220ms slower than doing the method call here in the filter. Twig seems to be much more efficient at executing filters than at executing methods of an object.

Parameters

ChoiceView $choice The choice to check
string|array $selectedValue The selected value to compare

Return Value

bool Whether the choice is selected

See also

ChoiceView::isSelected()
doc_Symfony
2016-10-28 06:18:34
Comments
Leave a Comment

Please login to continue.