Installation:
composer require boruta/star-rating-bundle
Enable the bundle in config/bundles.php:
Boruta\StarRatingBundle\StarRatingBundle::class => ['all' => true],
Twig Configuration:
Add the Twig paths in config/packages/twig.yaml:
twig:
paths:
'%kernel.project_dir%/vendor/boruta/star-rating-bundle/Resources/views': BorutaStarRatingBundle
Assets:
Include CSS/JS in your base template (e.g., base.html.twig):
<link rel="stylesheet" href="{{ asset('bundles/starrating/css/rating.css') }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="{{ asset('bundles/starrating/js/rating.js') }}"></script>
First Render: Use the bundle in a Twig template:
{{ render_star_rating({ value: 3, max: 5, read_only: true }) }}
For an interactive rating:
{{ render_star_rating({ value: 3, max: 5, read_only: false, on_change: 'updateRating(event)' }) }}
read_only: true for display-only ratings (e.g., product reviews):
{{ render_star_rating({ value: 4.5, max: 5, read_only: true }) }}
read_only: false and specify a callback:
{{ render_star_rating({
value: 2,
max: 5,
read_only: false,
on_change: 'handleRatingChange(event, rating)'
}) }}
JavaScript Handler:
function handleRatingChange(event, rating) {
fetch('/api/rate', {
method: 'POST',
body: JSON.stringify({ rating: rating }),
headers: { 'Content-Type': 'application/json' }
});
}
// src/Form/StarRatingType.php
use Boruta\StarRatingBundle\Form\StarRatingType;
class CustomStarRatingType extends StarRatingType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'max' => 5,
'read_only' => false,
]);
}
}
Use in a form:
{{ form_row(form.star_rating, { attr: { 'data-on-change': 'updateForm(event)' } }) }}
UpsertEntityFormType or custom controllers:
// src/Controller/RatingController.php
public function update(RatingRequest $request, RatingEntity $rating): Response {
$rating->setValue($request->get('rating'));
$em->persist($rating);
$em->flush();
return new JsonResponse(['success' => true]);
}
templates/BorutaStarRatingBundle/star_rating.html.twig
Example customization:
{% extends 'BorutaStarRatingBundle:star_rating.html.twig' %}
{% block star_icon %}
<i class="fas fa-star" style="color: {{ value <= loop.index0 ? '#ffc107' : '#ddd' }}"></i>
{% endblock %}
jQuery Dependency:
package.json or include the exact CDN link:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
Asset Paths:
bundles/starrating/) may fail in custom setups.asset() consistently or override paths via Twig’s paths config.CSRF in AJAX:
fetch('/api/rate', {
method: 'POST',
body: JSON.stringify({ rating: rating, _token: '{{ csrf_token('rating_submit') }}' }),
headers: { 'Content-Type': 'application/json' }
});
Font Awesome Missing:
npm install font-awesome
Then include in your layout:
<link rel="stylesheet" href="{{ asset('node_modules/font-awesome/css/font-awesome.min.css') }}">
Console Errors:
Uncaught ReferenceError: $ is not defined → Missing jQuery.404 on rating.css/rating.js → Verify asset() paths or Twig config.Event Not Triggering:
on_change is correctly bound in Twig:
{{ render_star_rating({ on_change: 'myFunction(event, rating)' }) }}
Half-Stars Not Working:
value: 4.5). If not rendering:
half classes or override the Twig template to include:
{% if value >= loop.index0 + 0.5 %}
<i class="fas fa-star"></i>
{% elseif value > loop.index0 - 0.5 %}
<i class="fas fa-star-half-alt"></i>
{% else %}
<i class="far fa-star"></i>
{% endif %}
Custom Icons:
{% block star_icon %}
<svg class="star {{ 'filled' if value >= loop.index0 else 'empty' }}">
{# SVG path data #}
</svg>
{% endblock %}
Validation:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\GreaterThanOrEqual(1)
* @Assert\LessThanOrEqual(5)
*/
private $rating;
Server-Side Rendering:
{% if app.environment == 'dev' %}
{{ include('BorutaStarRatingBundle:star_rating.js') }}
{% endif %}
Localization:
{{ 'star_rating.label'|trans({ 'max': max }, 'messages') }}
How can I help you explore Laravel packages today?