【问题标题】:CakePHP - Hotw to Generate Select Field with Mutiple Attribute using FormHelperCakePHP - 如何使用 FormHelper 生成具有多个属性的选择字段
【发布时间】:2020-05-07 10:24:58
【问题描述】:

我需要找到一种方法来设置具有multiple 属性的select-type 字段的默认值。

这是通过控制器发送到视图的数据:

$categories = [
    ['id' => 1, 'description' => 'Hardware'],
    ['id' => 2, 'description' => 'Sofware'],
    ['id' => 3, 'description' => 'Peopleware'],
    ['id' => 4, 'description' => 'Alienware'],
];
$selectedCategoriesIds = [1, 3];
$this->set(compact('categories', 'selectedCategoriesIds'));

视图看起来像这样:

<select name="categories[_ids][]" multiple="multiple">
    <?php foreach ($categories as $category): ?>
    <option value="<?= $category->id ?>"<?= (in_array($category->id, $selectedCategoriesIds) ? 'selected' : '') ?>><?= $category->description ?></option>
    <?php endforeach; ?>
</select>

这是视图中生成的 HTML:

    <select name="categories[_ids][]" multiple="multiple">
        <option value="1" selected>Hardware</option>
        <option value="2">Software</option>
        <option value="3" selected>Peopleware</option>
        <option value="4">Alienware</option>
    </select>

一切正常,我的问题是我是否可以使用 CakePHP 的 FormHelper 获得相同的结果,所以我不需要遍历 $categories 并在视图内调用 in_array()。我已经查阅了食谱,但我没有找到任何东西,或者不明白在这种特定情况下如何去做。我认为它会是这样的:

<?= $this->Form->control('categories._ids', ['some params']) ?>

谢谢。

【问题讨论】:

标签: php cakephp form-helpers cakephp-4.x


【解决方案1】:

如果您的 $categories 数组的结构稍有不同,您应该可以使用表单类。 Cookbook 有一个例子here:

// HTML <option> elements with values 1 and 3 will be rendered preselected
echo $this->Form->select(
    'rooms',
    [1, 2, 3, 4, 5],
    [
        'multiple' => true,
        'value' => [1, 3]
    ]
);

首先,只需将 $categories 映射到类别 ID => 描述的更简单映射列表。

如果此类别列表是数据库中的数据,只需使用“列表”而不是默认方法选择它,例如:

$categories = $CategoriesTable->find('list');

但是,如果它不是来自查询结果,您仍然可以手动转换您的数组:

$categories = [
    ['id' => 1, 'description' => 'Hardware'],
    ['id' => 2, 'description' => 'Sofware'],
    ['id' => 3, 'description' => 'Peopleware'],
    ['id' => 4, 'description' => 'Alienware'],
];

$formattedCategories = [];
foreach($categories as $row){
    $formattedCategories[$row['id']] = $row['description'];
}
$categories = $formattedCategories;

一旦采用这种格式,常规的Form-&gt;select 将起作用:

echo $this->Form->select(
    'categories[_ids][]',
    $categories,
    [
        'multiple' => true,
        'value' => $selectedCategoriesIds
    ]
);

【讨论】:

  • 您的示例生成了这样的选项标签:,但我需要的是这样的:
  • 我刚刚再次测试了我的代码,它生成了您需要的 HTML,这就是我所看到的:&lt;select name="categories[_ids][][]" multiple="multiple" class="form-control"&gt;&lt;option value="1" selected="selected"&gt;Hardware&lt;/option&gt;&lt;option value="2"&gt;Sofware&lt;/option&gt;&lt;option value="3" selected="selected"&gt;Peopleware&lt;/option&gt;&lt;option value="4"&gt;Alienware&lt;/option&gt;&lt;/select&gt; 你得到不同的结果了吗?
  • $CategoriesTable->find('list') 只返回 ids,我用 $CategoriesTable->find('all', ['fields' => ['id', 'description'] ) 并且成功了!
  • 啊完美!您也可以只更改 CategoriesTable 中的默认“显示字段”,它通常默认为 ID。在 initialize 函数内,确保它显示 $this-&gt;setDisplayField('description'); - 这是 find('list') 返回的内容。
猜你喜欢
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 2023-03-13
  • 2014-07-24
  • 1970-01-01
相关资源
最近更新 更多