Skip to content

Commit

Permalink
Merge pull request #56 from m-vo/bugfix/fix-rating-type-off-by-one
Browse files Browse the repository at this point in the history
Fix rating type off by ones
  • Loading branch information
m-vo authored Nov 16, 2021
2 parents 184f237 + cd81f73 commit f6bf63d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Entity/AnswerRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function setRating(?int $rating): void

public function exportData(DataContainer $container): void
{
$container->setValue($this->getRating());
// Use 1-based index
$container->setValue($this->getRating() + 1);
}
}
2 changes: 2 additions & 0 deletions src/Form/AnswerType/AnswerRatingType.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'expanded' => true,
'required' => $question->isMandatory(),
'constraints' => $question->isMandatory() ? [new NotBlank()] : [],
// Disable placeholder, so we're not getting additional options
'placeholder' => null,
]);
}

Expand Down
7 changes: 3 additions & 4 deletions src/Resources/views/Form/Rating.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
{% block form_row %}
<div class="{% block answer_classes %}survey_answer survey_answer--rating{% endblock %}">
{% set options = form.rating.children %}
{% set activeOption = form.rating.children|filter(c => c.vars.checked)|first|default(0) %}
{% set activeOption = options|filter(c => c.vars.checked)|first.vars.value|default(-1) %}

{# Star rating, rendered as radio inputs where selecting option `n` marks options `1..n-1` as active #}
<div class="{% block rating_wrapper_classes %}flex flex-row space-x-1{% endblock %}"
x-data="{value: {{ activeOption ? activeOption.vars.value : 0 }}}">
x-data="{value: {{ activeOption }}}">
{% for option in options %}
{% set value = option.vars.value|default(0) %}
{% set value = option.vars.value %}
{% set label = option.vars.label %}

<div class="{% block input_wrapper_classes %}{% endblock %}">
<input class="{% block input_classes %}hidden{% endblock %}" type="radio" id="{{ option.vars.id }}" name="{{ option.vars.full_name }}"
value="{{ value }}" :class="{'active': value>={{ value }}}" x-model="value" @reset="value=0"
Expand Down

0 comments on commit f6bf63d

Please sign in to comment.