Search code examples
phpyii2ratingkartik-v

Yii2 - Kartik Widget-Rating overwrite values


it's possible overwrite the values of each star? i need define step = 3.75, min = 0 and max = 15, and prevent from selecting half a star. the values I want are:

star 1 => 0; star 2 => 3.75; star 3 => 7.5; star 4 => 11,25; star 5 => 15.

form.php

echo $form->field($model, 'rating')->widget(StarRating::classname(), [
    'pluginOptions' => [
    'stars' => 5, 
    'step' => 3.75,
    'min' => 0,
    'max' => 15,
    ]
]);

but when i make this, the selection of each star not display correctly, half of star is selected.


Solution

  • I thought you only want to display the client side (with manual numbers). And you will not get numbers from DB
    the correct way is to overwrite the star-rating file. Which should be referred to the Kartik forum.
    You must overwrite that the first star has a zero score (highlight first star = 0)
    However, you can use the following code in your model.

    public function beforeSave($insert)
    {
        // if ($insert) {  // only for Save (No Update)
            if (!empty($this->Your_field)) {
                $this->setAttribute('Your_field', $this->Your_field-3.75);
            }
        // }
        return parent::beforeSave($insert);
    }
    

    Instead of your_field, enter your field name (field of the rate).