简体   繁体   中英

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

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:

管理区

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM