表单
Textarea
简介
文本框允许你使用多行字符串:
use Filament\Forms\Components\Textarea;
Textarea::make('description')

调整文本框大小
通过定义 rows()
和 cols()
方法你可用修改文本框的大小:
use Filament\Forms\Components\Textarea;
Textarea::make('description')
->rows(10)
->cols(20)
除了静态值,rows()
和 cols()
方法同时也接受通过函数动态设置其值。你可以将多个 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. |
自动调整文本框大小
你可以通过设置 autosize()
方法来允许文本框自动调整大小以适应其内容:
use Filament\Forms\Components\Textarea;
Textarea::make('description')
->autosize()
可选地,你可以传递一个布尔值来控制文本框是否应自动调整大小:
use Filament\Forms\Components\Textarea;
Textarea::make('description')
->autosize(FeatureFlag::active())
除了允许静态值外,autosize()
方法还接受一个函数来动态计算。你可以将各种 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. |
将字段设为只读
使用 readOnly()
方法你可以将字段设为“只读”。需要注意的是,只读不同于禁用字段。
use Filament\Forms\Components\Textarea;
Textarea::make('description')
->readOnly()
相较于 disabled()
,它有一些差异:
- 使用
readOnly()
时,该字段仍会在提交表单时发送到服务器。你可以通过浏览器控制台或者 JavaScript 修改其值。不过,你可以通过使用dehydrated(false)
来阻止修改。 - 使用
readOnly()
时,不会产生样式变化,比如减少透明度。 - 当使用
readOnly()
时,字段仍然是可聚焦的。
此外,你可以传递布尔值控制该字段是否设为只读:
use Filament\Forms\Components\Textarea;
Textarea::make('description')
->readOnly(FeatureFlag::active())
除了允许静态值外,readOnly()
方法还接受一个函数来动态计算。你可以将各种 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. |
禁用 Grammarly 检查
如果用户安装了 Grammarly 并且你想要阻止它分析文本框中的内容,你可以使用 disableGrammarly()
方法:
use Filament\Forms\Components\Textarea;
Textarea::make('description')
->disableGrammarly()
此外,你可以传递布尔值控制该字段是否禁用 Grammarly 语法检测:
use Filament\Forms\Components\Textarea;
Textarea::make('description')
->disableGrammarly(FeatureFlag::active())
除了允许静态值外,disableGrammarly()
方法还接受一个函数来动态计算。你可以将各种 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. |
文本框验证
除了验证页面中列出的那些规则之外,还有一些特定于文本框的其他规则。
长度验证
通过设置 minLength()
和 maxLength()
方法,你可以限制文本框的长度。这两个方法同时添加了前端验证和后端验证:
use Filament\Forms\Components\Textarea;
Textarea::make('description')
->minLength(2)
->maxLength(1024)
你也可以通过设置 length()
来指定文本框的准确长度。该方法同样也添加了前端和后端验证:
use Filament\Forms\Components\Textarea;
Textarea::make('question')
->length(100)
除了允许静态值外,minLength()
、maxLength()
和 length()
方法也接受通过函数来动态计算。你可以将各种 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. |
Still need help? Join our Discord community or open a GitHub discussion