Add custom rating star to a form field in sonata configureFormFileds method

477 Views Asked by At

I am using sonata admin and I have created a custom form field type following these instructions:symfony_3.4_custom_form_field. I can't figure out how to link my custom template for rating stars to the field rate in ConfigureFormFields method. Here are my methods:

    <?php


class RatingType extends AbstractType
{
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        //foreach (range(1, 5) as $range) {
            $resolver->setDefaults([
                'choices' => [
                    'rating_1' => 1,
                    'rating_2' => 2,
                    'rating_3' => 3,
                    'rating_4' => 4,
                    'rating_5' => 5,
                ],
                'choices_as_values' => true,
                //'attr' => ['style' => "font-family: 'FontAwesome'"],
            ]);
        //}
    }

    /**
     * @return null|string
     */
    public function getParent()
    {
        return ChoiceType::class;
    }

}

protected function configureFormFields(FormMapper $formMapper)

{

        $formMapper
            ->add('rate', RatingType::class, [
                'required' => false,
                'label' => 'Rating',
                'expanded' => true,
            ])
            ->end()

}

Custom template fields_admin.html.twig:
<form>
            <ul class="rating-choices"> 
                <li><label for="rating_1"><i class="fa fa-star" aria-hidden="true"></i></label><input type="radio" name="ratings" id="rating_1" value="1"/></li>
                <li><label for="rating_2"><i class="fa fa-star" aria-hidden="true"></i></label><input type="radio" name="ratings" id="rating_2" value="2"/></li>
                <li><label for="rating_3"><i class="fa fa-star" aria-hidden="true"></i></label><input type="radio" name="ratings" id="rating_3" value="3"/></li>
                <li><label for="rating_4"><i class="fa fa-star" aria-hidden="true"></i></label><input type="radio" name="ratings" id="rating_4" value="4"/></li>
                <li><label for="rating_5"><i class="fa fa-star" aria-hidden="true"></i></label><input type="radio" name="ratings" id="rating_5" value="5"/></li>
            </ul>
    </form>

Current Default Result:

admin area

1

There are 1 best solutions below

1
Moubarak Hayal On

Well i dont know how to link them in the right way but I might have an alternative for you.

You can use this packege to add rating stras to your system instead of the custom made ones. StarRatingBundle

Hope this helps