range slider from kartik

Hi all,

I try to use the range slider from kartik.

For one value (in this case it’s not a range but only a slider of course) all is ok but when I try to use a range I don’t know how to ‘save’ the values (min, max).

in my view :


<?php $form = ActiveForm::begin(['layout' => 'horizontal',...

<?= $form->field($model, 'years')->label('Years .')->widget(Slider::classname(), [

    'name' => 'slider_years',

    'sliderColor' => Slider::TYPE_GREY,

    'pluginOptions'=>[

        'handle' => 'round',

        'min'=>2000,

        'max'=>2014,

        'step'=>1,

        'tooltip'=>'always',

        'range'=>true

    ],

]); ?>

...

<?php ActiveForm::end(); ?> 



in my model:




class Formrequest extends Model

{

    public $years;

}



My problem: when I validate the form, I don’t how to ‘save’ the both values of the range slider

do you get any error…?

I don’t see any error, I just would like to know how to save the values of the slider.

I mean, for a single slider I know how to do that but for a range slider, because it’s an array of values I don’t know how to get the values.

With others words, I look for an example of using a range slider with an activeForm.

You may want to read the documentation for the extension. As mentioned in the Settings section:

If you are using a range select, you need to pass the 2 range values concatenated with a , (comma). Alternatively, if you have passed a single numeric value and set pluginOptions[‘range’] to true, then pluginOptions[‘max’] will be used as the second value.

So while retrieving values, you will get a string with the 2 values separated by a comma. You can retrieve the two values using any string separation methods accordingly… for example:




// read value posted from active form to your model input

$values = explode(",", $model->slider_input_id);


// the range values can be saved in separate attributes like this

$model->range_from = $values[0]; // from part of the range

$model->range_to = $values[1]; // to part of the range



Thanks for your reply,

I have already read the documentation and I don’t see how to retrieve the values.

In your example, you said :


// read value posted from active form to your model input

$values = explode(",", $model->slider_input_id);


// the range values can be saved in separate attributes like this

$model->range_from = $values[0]; // from part of the range

$model->range_to = $values[1]; // to part of the range

if I try with my code, it will be :


$values = explode(",", $model->slider_years);

–> makes non sense, it would be :


$values = explode(",", $model->years);

–> gives me empty value

Strange, no ?

Perhaps, my problem is in my model declaration (I didn’t writen rules for this value)…

What do you suggest ?

Another information:

If I give default values, ie:


<?= $form->field($model, 'years')->label('Years .')->widget(Slider::classname(), [

    'name' => 'slider_years',

    'sliderColor' => Slider::TYPE_GREY,

    'value' => '2002,2013',

    'pluginOptions'=>[

        'handle' => 'round',

        'min'=>2000,

        'max'=>2014,

        'step'=>1,

        'tooltip'=>'always',

        'range'=>true

    ],

]); ?>



–> theses default values are not taken in account !