templates/commentaires/_form.html.twig line 1
{{ form_start(form) }}<div class="w-full mt-4 border border-gray-200 rounded-lg bg-gray-50 dark:bg-gray-700 dark:border-gray-600"><div class="px-4 py-2 bg-white rounded-t-lg dark:bg-gray-800"><label for="comment" class="sr-only text-left">Votre commentaire</label>{{ form_widget(form) }}</div><div class="flex items-center justify-between px-3 py-2 border-t dark:border-gray-600"><button type="submit" class="inline-flex items-center py-2.5 px-4 text-xs font-medium text-center text-white bg-prim-700 rounded-lg focus:ring-4 focus:ring-prim-200 dark:focus:ring-prim-900 hover:bg-prim-800">Poster</button><div class="flex items-center space-x-1">{% for starValue in 1..5 %}<svg class="w-4 h-4 text-gray-300 star" data-value="{{ starValue }}" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewbox="0 0 22 20"><path d="M20.924 7.625a1.523 1.523 0 0 0-1.238-1.044l-5.051-.734-2.259-4.577a1.534 1.534 0 0 0-2.752 0L7.365 5.847l-5.051.734A1.535 1.535 0 0 0 1.463 9.2l3.656 3.563-.863 5.031a1.532 1.532 0 0 0 2.226 1.616L11 17.033l4.518 2.375a1.534 1.534 0 0 0 2.226-1.617l-.863-5.03L20.537 9.2a1.523 1.523 0 0 0 .387-1.575Z"/></svg>{% endfor %}</div></div></div><input type="hidden" value="" id="rating" name="rating">{{ form_end(form) }}<script>const stars = document.querySelectorAll('.star');let selectedStars = 0;stars.forEach(star => {star.addEventListener('click', () => {selectedStars = parseInt(star.getAttribute('data-value'));updateStars();});});function updateStars() {stars.forEach((star, index) => {if (index < selectedStars) {star.classList.add('text-yellow-300');star.classList.remove('text-gray-300');} else {star.classList.add('text-gray-300');star.classList.remove('text-yellow-300');}});calculateRating();}let nbRating = 0;const calculateRating = () => {nbRating = 0;stars.forEach(star => {console.log(star.classList.contains('text-yellow-300'));if (star.classList.contains('text-yellow-300')) {nbRating++;}document.querySelector('#rating').value = nbRating;})}</script>