表单
Slider
简介
滑块(Slider)组件允许你通过滑动选择一个或多个数值:
use Filament\Forms\Components\Slider;
Slider::make('slider')
该组件使用了 noUiSlider 包,其中的很多 API 也是基于该库。
NOTE
由于其性质,滑块字段永远不会为空。字段值永远不能为 null 或空数组。如果滑块字段为空,则用户将没有能在轨道上拖动的控制柄。
因此,滑块字段有一个开箱即用的默认值,该值设置为滑块的范围中允许的最小值。默认值在表单为空时使用,例如在资源的“新建”页面上。要了解有关默认值的更多信息,请查看 default() 文档。
控制滑块范围
滑块默认可选的最小值和最大值为 0 和 100。Filament 将会自动应用验证规则以确保用户不会超出这些值。你可以使用 range() 方法对其进行修改:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 40, maxValue: 80)
除了静态值外,range() 方法同时也接受通过函数动态设置其值。你可以将各种 utility 作为参数注入到该函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
控制步长
默认情况下,用户可以选择最小值和最大值之间的任何十进制值。你可以使用 step() 方法将值限制为特定的步长。Filament 将自动应用验证规则,以确保用户不能偏离此步长:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->step(10)
除了静态值外,step() 方法同时也接受通过函数动态设置其值。你可以将各种 utility 作为参数注入到该函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
限制小数位数
如果你希望允许用户选择最小值和最大值之间的任何十进制值,而不是将它们限制在特定的步长内,你可以定义一个小数位数,使用 decimalPlaces() 方法将其所选值四舍五入:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->decimalPlaces(2)
除了静态值外,decimalPlaces() 方法同时也接受通过函数动态设置其值。你可以将各种 utility 作为参数注入到该函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
Controlling the behavioral padding of the track
By default, users can select any value across the entire length of the track. You can add behavioral padding to the track using the rangePadding() method. This will ensure that the selected value is always at least a certain distance from the edges of the track. The minimum and maximum value validation that Filament applies to the slider by default will take the padding into account to ensure that users cannot exceed the padded range:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->rangePadding(10)
除了允许静态值之外,rangePadding() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
此示例中,即使最小值为 0,最大值为 100,用户也只能选择 10 到 90 之间的值。内边距(padding)将应用于轨道的两端,因此所选值始终距离轨道边缘至少 10 个单位。
如果你想分别控制轨道两侧的内边距,可以将一个包含两个值的数组传递给 rangePadding() 方法。第一个值将应用于轨道的起始位置,第二个值将应用于轨道的结束位置:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->rangePadding([10, 20])
Right-to-left tracks
By default, a track operates left-to-right. If the user is using a right-to-left locale (e.g. Arabic), the track will be displayed right-to-left automatically. You can also force the track to be displayed right-to-left using the rtl() method:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->rtl()
Optionally, you may pass a boolean value to control if the slider should be right-to-left or not:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->rtl(FeatureFlag::active())
除了允许静态值之外,rtl() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
向 Slider 添加多个值
如果 Slider 设置为值数组,用户将能够在允许的范围内拖动多个滑块。请确保滑块的 default() 值 设置为值数组,以便在表单为空时使用:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->default([20, 70])
如果你使用 Eloquent 报错多个值,你应该确保添加 array cast到该模型属性中:
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $casts = [
'slider' => 'array',
];
// ...
}
使用垂直轨道
你可以将 Slider 显示为垂直轨道而不是水平轨道,请使用 vertical() 方法:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->vertical()
此外,你也可以传入布尔值用以控制滑块是否垂直:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->vertical(FeatureFlag::active())
除了允许静态值之外,vertical() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
Top-to-bottom tracks
By default, a vertical track operates bottom-to-top. In noUiSlider, this is the right-to-left behavior. To assign the minimum value to the top of the track, you can use the rtl(false) method:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->vertical()
->rtl(false)
Adding tooltips to handles
You can add tooltips to the handles of the slider using the tooltips() method. The tooltip will display the current value of the handle:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->tooltips()
Optionally, you may pass a boolean value to control if the slider should have tooltips or not:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->tooltips(FeatureFlag::active())
When using multiple handles, multiple tooltips will be displayed, one for each handle. Tooltips also work with vertical tracks.
自定义 Tooltip 格式化
You can use JavaScript to customize the formatting of a tooltip. Pass a RawJs object to the tooltips() method, containing a JavaScript string expression to use. The current value to format will be available in the $value variable:
use Filament\Forms\Components\Slider;
use Filament\Support\RawJs;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->tooltips(RawJs::make(<<<'JS'
`$${$value.toFixed(2)}`
JS))
Controlling tooltips for multiple handles individually
If the slider is set to an array of values, you can control the tooltips for each handle separately by passing an array of values to the tooltips() method. The first value will be applied to the first handle, and the second value will be applied to the second handle, and so on:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->tooltips([true, false])
Filling a track with color
By default, the color of the track is not affected by the position of any handles it has. When using an individual handle, you can fill the part of the track before the handle with color using the fillTrack() method:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->fillTrack()
Optionally, you may pass a boolean value to control if the slider should be filled or not:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->fillTrack(FeatureFlag::active())
除了允许静态值之外,fillTrack() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
When using multiple handles, you must manually specify which parts of the track should be filled by passing an array of true and false values, one for each section. The total number of values should be one more than the number of handles. The first value will be applied to the section before the first handle, the second value will be applied to the section between the first and second handles, and so on:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->fillTrack([false, true, false])
Filling also works on vertical tracks:
Adding pips to tracks
You can add pips to tracks, which are small markers that indicate the position of the handles. You can add pips to the track using the pips() method:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->pips()
Pips also work when there are multiple handles:
You can also add pips to vertical tracks:
Adjusting the density of pips
By default, pips are displayed at a density of 10. This means that for every 10 units of the track, a pip will be displayed. You can adjust this density using the density parameter of the pips() method:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->pips(density: 5)
除了允许静态值之外,density 参数也接受函数动态计算它的值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
Custom pip label formatting
You can use JavaScript to customize the formatting of a pip label. Pass a RawJs object to the pipsFormatter() method, containing a JavaScript string expression to use. The current value to format will be available in the $value variable:
use Filament\Forms\Components\Slider;
use Filament\Support\RawJs;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->pips()
->pipsFormatter(RawJs::make(<<<'JS'
`$${$value.toFixed(2)}`
JS))
除了允许静态值之外,pipsFormatter() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
Adding pip labels to steps of the track
If you are using steps to restrict the movement of the track, you can add a pip label to each step. To do this, pass a PipsMode::Steps object to the pips() method:
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\Slider\Enums\PipsMode;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->step(10)
->pips(PipsMode::Steps)
If you would like to add additional pips to the track without labels, you can adjust the density of the pips as well:
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\Slider\Enums\PipsMode;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->step(10)
->pips(PipsMode::Steps, density: 5)
Adding pip labels to percentage positions of the track
If you would like to add pip labels to the track at specific percentage positions, you can pass a PipsMode::Positions object to the pips() method. The percentage positions need to be defined in the pipsValues() method in an array of numbers:
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\Slider\Enums\PipsMode;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->pips(PipsMode::Positions)
->pipsValues([0, 25, 50, 75, 100])
除了允许静态值之外,pipsValues() 方法也接受函数来动态计算它们的值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
The density still controls the spacing of the pips without labels.
Adding a set number of pip labels to the track
If you would like to add a set number of pip labels to the track, you can pass a PipsMode::Count object to the pips() method. The number of pips need to be defined in the pipsValues() method:
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\Slider\Enums\PipsMode;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->pips(PipsMode::Count)
->pipsValues(5)
除了允许静态值之外,pipsValues() 方法也接受函数来动态计算它们的值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
The density still controls the spacing of the pips without labels.
Adding pip labels to set values on the track
Instead of defining percentage positions or a set number of pip labels, you can also define a set of values to use for the pip labels. To do this, pass a PipsMode::Values object to the pips() method. The values need to be defined in the pipsValues() method in an array of numbers:
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\Slider\Enums\PipsMode;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->pips(PipsMode::Values)
->pipsValues([5, 15, 25, 35, 45, 55, 65, 75, 85, 95])
除了允许静态值之外,pipsValues() 方法也接受函数来动态计算它们的值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
The density still controls the spacing of the pips without labels:
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\Slider\Enums\PipsMode;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->pips(PipsMode::Values, density: 5)
->pipsValues([5, 15, 25, 35, 45, 55, 65, 75, 85, 95])
Manually filtering pips
For even more control over how pips are displayed on a track, you can use a JavaScript expression. The JavaScript expression should return different numbers based on the pip’s appearance:
- The expression should return
1if a large pip label should be displayed. - The expression should return
2if a small pip label should be displayed. - The expression should return
0if a pip should be displayed without a label. - The expression should return
-1if a pip should not be displayed at all.
The density of the pips will control which values get passed to the JavaScript expression. The expression should use the $value variable to access the current value of the pip. The expression should be defined in a RawJs object and passed to the pipsFilter() method:
use Filament\Forms\Components\Slider;
use Filament\Support\RawJs;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->pips(density: 5)
->pipsFilter(RawJs::make(<<<'JS'
($value % 50) === 0
? 1
: ($value % 10) === 0
? 2
: ($value % 25) === 0
? 0
: -1
JS))
除了允许静态值之外,pipsFilter() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
In this example the % operator is used to determine the divisibility of the pip value. The expression will return 1 for every 50 units, 2 for every 10 units, 0 for every 25 units, and -1 for all other values:
Setting a minimum difference between handles
To set a minimum distance between handles, you can use the minDifference() method, passing a number to it. This represents the real difference between the values of both handles, not their visual distance:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->minDifference(10)
除了允许静态值之外,minDifference() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
NOTE
The minDifference() method does not impact the validation of the slider. A skilled user could manipulate the value of the slider using JavaScript to select a value that does not align with the minimum difference. They will still be prevented from selecting a value outside the range of the slider.
Setting a maximum difference between handles
To set a maximum distance between handles, you can use the maxDifference() method, passing a number to it. This represents the real difference between the values of both handles, not their visual distance:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->maxDifference(40)
除了允许静态值之外,maxDifference() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
NOTE
The maxDifference() method does not impact the validation of the slider. A skilled user could manipulate the value of the slider using JavaScript to select a value that does not align with the maximum difference. They will still be prevented from selecting a value outside the range of the slider.
控制滑块的常规行为
滑块的 behavior() 方法允许你传递一个或多个 Behavior 对象来控制滑块的行为。可用选项包括:
Behavior::Tap- 当用户点击轨道时,滑块将平滑地移动到点击位置。这是默认行为,因此在应用其他行为时,如果要保留此行为,也应将此行为包含在数组中。Behavior::Drag- 当轨道上有两个手柄时,用户可以拖动轨道来同时移动两个手柄。必须使用fillTrack([false, true, false])方法来确保用户有可拖动的内容。Behavior::Drag和Behavior::Fixed- 当轨道上有两个手柄时,用户可以拖动轨道同时移动两个手柄,但无法改变它们之间的距离。必须使用fillTrack([false, true, false])方法来确保用户有可拖动的对象。请注意,后端不会自动验证手柄之间的距离,因此熟练的用户可以使用 JavaScript 操纵滑块的值,选择具有不同距离的值。但他们仍然无法选择滑块范围之外的值。Behavior::Unconstrained- 当轨道上有多个手柄时,它们可以相互拖动。minDifference()和maxDifference()方法不适用于此行为。Behavior::SmoothSteps- 当拖动手柄时,它不会捕捉到轨道的步长。用户释放手柄后,它将捕捉到最近的步长。
例如,要同时使用 Behavior::Tap、Behavior::Drag 和 Behavior::SmoothSteps:
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\Slider\Enums\Behavior;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->behavior([Behavior::Tap, Behavior::Drag, Behavior::SmoothSteps])
要禁用所有行为,包括默认的 Behavior::Tap,你可以使用 behavior(null) 方法:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->behavior(null)
非线性轨迹
你可以通过在滑块的 nonLinearPoints() 方法中定义一个百分比点数组来创建非线性轨迹,其中轨迹的某些部分会被压缩或扩展。数组中的每个百分比键都应该有一个对应的实数值,该值将用于计算滑块在轨道上的位置。以下示例以 pips 为例,演示了轨道的非线性行为:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->nonLinearPoints(['20%' => 50, '50%' => 75])
->pips()
除了允许静态值之外,nonLinearPoints() 方法也接受函数来动态计算它们的值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.
| Utility | Type | Parameter | Description |
|---|---|---|---|
| Field | Filament\Forms\Components\Field | $component | The current field component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
| Livewire | Livewire\Component | $livewire | The Livewire component instance. |
| Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
| Operation | string | $operation | The current operation being performed by the schema. Usually create, edit, or view. |
| Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
| State | mixed | $state | The current value of the field. Validation is not run. |
When using a non-linear track, you can also control the stepping for each section. By defining an array of two numbers for each percentage point, the first number will be used as the real value for percentage position, and the second number will be used as the step size for that section, active until the next percentage point:
use Filament\Forms\Components\Slider;
Slider::make('slider')
->range(minValue: 0, maxValue: 100)
->nonLinearPoints(['20%' => [50, 5], '50%' => [75, 1]])
->pips()
NOTE
When using a non-linear track, the step values of certain track sections do not affect the validation of the slider. A skilled user could manipulate the value of the slider using JavaScript to select a value that does not align with a step value in the track. They will still be prevented from selecting a value outside the range of the slider.
When using pips with a non-linear track, you can ensure that pip labels are rounded and only displayed at selectable positions on the track. Otherwise, the stepping of a non-linear portion of the track could add labels to positions that are not selectable. To do this, use the steppedPips() method:
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\Slider\Enums\PipsMode;
Slider::make('slider')
->range(minValue: 0, maxValue: 10000)
->nonLinearPoints(['10%' => [500, 500], '50%' => [4000, 1000]])
->pips(PipsMode::Positions, density: 4)
->pipsValues([0, 25, 50, 75, 100])
->steppedPips()
Edit on GitHubStill need help? Join our Discord community or open a GitHub discussion