CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识 & 商标
  • 商业解决方案
  • 周边产品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与
    • 问题 (Github)
    • 烘焙坊
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 新闻稿
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助 & 支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • 缓存
    • 集合
    • 命令
    • 控制台
    • 控制器
    • 核心
    • 数据库
    • 数据源
    • 错误
    • 事件
    • 表单
    • Http
    • I18n
    • 日志
    • 邮件
    • 网络
    • ORM
    • 路由
    • 测试套件
    • 实用工具
    • 验证
    • 视图
      • 异常
      • 表单
      • 助手
      • 小部件

类 SelectBoxWidget

用于生成下拉框的输入小部件类。

此类通常由 Cake\View\Helper\FormHelper 内部使用,但可以用于生成独立的下拉框。

命名空间: Cake\View\Widget

属性摘要

  • $_templates protected
    Cake\View\StringTemplate

    StringTemplate 实例。

  • $defaults protected
    array<string, mixed>

    数据默认值。

方法摘要

  • __construct() public

    构造函数。

  • _emptyValue() protected

    根据输入生成空值。

  • _isDisabled() protected

    用于决定哪些选项被禁用的辅助方法。

  • _isSelected() protected

    用于决定哪些选项被选中的辅助方法。

  • _renderContent() protected

    渲染 select 元素的内容。

  • _renderOptgroup() protected

    渲染 optgroup 元素的内容。

  • _renderOptions() protected

    渲染一组选项。

  • mergeDefaults() protected

    将默认值与提供的數據合并。

  • render() public

    渲染一个下拉框表单输入。

  • secureFields() public

    返回此小部件需要安全保护的字段列表。

  • setMaxLength() protected

    如果适用,设置 "maxlength" 属性的值。

  • setRequired() protected

    如果适用,设置 "required" 属性的值。

  • setStep() protected

    如果适用,设置 "step" 属性的值。

方法详情

__construct() ¶ public

__construct(Cake\View\StringTemplate $templates)

构造函数。

参数
Cake\View\StringTemplate $templates

模板列表。

_emptyValue() ¶ protected

_emptyValue(array|string|bool $value): array

根据输入生成空值。

参数
array|string|bool $value

提供的空值。

返回值
array

_isDisabled() ¶ protected

_isDisabled(string $key, list<string>|null $disabled): bool

用于决定哪些选项被禁用的辅助方法。

参数
string $key

要测试的键。

list<string>|null $disabled

被禁用的值。

返回值
bool

_isSelected() ¶ protected

_isSelected(string $key, mixed $selected): bool

用于决定哪些选项被选中的辅助方法。

参数
string $key

要测试的键。

mixed $selected

被选中的值。

返回值
bool

_renderContent() ¶ protected

_renderContent(array<string, mixed> $data): list<string>

渲染 select 元素的内容。

参数
array<string, mixed> $data

渲染 select 的上下文。

返回值
list<string>

_renderOptgroup() ¶ protected

_renderOptgroup(string $label, ArrayAccess<string, mixed>|array<string, mixed> $optgroup, array|null $disabled, mixed $selected, array $templateVars, bool $escape): string

渲染 optgroup 元素的内容。

参数
string $label

optgroup 标签文本

ArrayAccess<string, mixed>|array<string, mixed> $optgroup

optgroup 数据。

array|null $disabled

要禁用的选项。

mixed $selected

要选中的选项。

array $templateVars

额外的模板变量。

bool $escape

切换 HTML 转义

返回值
string

_renderOptions() ¶ protected

_renderOptions(iterable $options, list<string>|null $disabled, mixed $selected, array $templateVars, bool $escape): list<string>

渲染一组选项。

当使用选项组时,将递归调用自身。

参数
iterable $options

要渲染的选项。

list<string>|null $disabled

要禁用的选项。

mixed $selected

要选中的选项。

array $templateVars

额外的模板变量。

bool $escape

切换 HTML 转义。

返回值
list<string>

mergeDefaults() ¶ protected

mergeDefaults(array<string, mixed> $data, Cake\View\Form\ContextInterface $context): array<string, mixed>

将默认值与提供的數據合并。

参数
array<string, mixed> $data

数据数组

Cake\View\Form\ContextInterface $context

上下文实例。

返回值
array<string, mixed>

render() ¶ public

render(array<string, mixed> $data, Cake\View\Form\ContextInterface $context): string

渲染一个下拉框表单输入。

根据一组数据渲染一个下拉框输入。支持的键为

  • name - 设置输入名称。
  • options - 选项数组。
  • disabled - 为 true 或要禁用的选项数组。当为 true 时,select 元素将被禁用。
  • val - 要标记为选中的字符串或选项数组。
  • empty - 设置为 true 以在选项元素的顶部添加一个空选项。设置为字符串以定义空选项的显示文本。如果使用数组,键将设置空选项的值,而值将设置显示文本。
  • escape - 设置为 false 以禁用 HTML 转义。

选项格式

options 选项可以采用多种数据格式,具体取决于您要生成的 HTML 的复杂程度。

您可以使用基本关联数组生成简单的选项

'options' => ['elk' => 'Elk', 'beaver' => 'Beaver']

如果您需要在选项元素上定义其他属性,可以使用选项的复杂形式

'options' => [
  ['value' => 'elk', 'text' => 'Elk', 'data-foo' => 'bar'],
]

此形式 **要求** 定义 value 和 text 键。如果两者都没有设置,选项将无法正确生成。

如果您需要定义选项组,可以使用嵌套数组

'options' => [
 'Mammals' => [
   'elk' => 'Elk',
   'beaver' => 'Beaver'
 ]
]

最后,如果您需要在 optgroup 元素上添加属性,可以使用更复杂的嵌套数组形式

'options' => [
  [
    'text' => 'Mammals',
    'data-id' => 1,
    'options' => [
      'elk' => 'Elk',
      'beaver' => 'Beaver'
    ]
 ],
]

您可以自由地将每种形式混合在同一个选项集中,并根据需要嵌套复杂类型。

参数
array<string, mixed> $data

要渲染的数据。

Cake\View\Form\ContextInterface $context

当前表单上下文。

返回值
string
抛出
RuntimeException
当 name 属性为空时。

secureFields() ¶ public

secureFields(array<string, mixed> $data): list<string>

返回此小部件需要安全保护的字段列表。

参数
array<string, mixed> $data
返回值
list<string>

setMaxLength() ¶ protected

setMaxLength(array<string, mixed> $data, Cake\View\Form\ContextInterface $context, string $fieldName): array<string, mixed>

如果适用,设置 "maxlength" 属性的值。

参数
array<string, mixed> $data

数据数组

Cake\View\Form\ContextInterface $context

上下文实例。

string $fieldName

字段名称。

返回值
array<string, mixed>

setRequired() ¶ protected

setRequired(array<string, mixed> $data, Cake\View\Form\ContextInterface $context, string $fieldName): array<string, mixed>

如果适用,设置 "required" 属性的值。

参数
array<string, mixed> $data

数据数组

Cake\View\Form\ContextInterface $context

上下文实例。

string $fieldName

字段名称。

返回值
array<string, mixed>

setStep() ¶ protected

setStep(array<string, mixed> $data, Cake\View\Form\ContextInterface $context, string $fieldName): array<string, mixed>

如果适用,设置 "step" 属性的值。

参数
array<string, mixed> $data

数据数组

Cake\View\Form\ContextInterface $context

上下文实例。

string $fieldName

字段名称。

返回值
array<string, mixed>

属性详情

$_templates ¶ protected

StringTemplate 实例。

类型
Cake\View\StringTemplate

$defaults ¶ protected

数据默认值。

类型
array<string, mixed>
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识 & 商标
  • 社区
  • 参与
  • 问题 (Github)
  • 烘焙坊
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 新闻稿
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助 & 支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成