CakePHP 3.x复选框格式化问题 [英] CakePHP 3.x checkbox formatting issue

查看:226
本文介绍了CakePHP 3.x复选框格式化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对你们来说很明显,但我真的很难找到一个正确的答案。我一般googled,以及阅读CakePHP手册和API以回答以下问题:

This may seem obvious to some of you, but I really am struggling to find a straight answer. I've generally googled, as well as read both the CakePHP manual and the API for an answer to the following question:

创建输入时,以下代码创建以下输出:

When creating an input, the following code creates the following outputs:

// in the view
echo $this->Form->input('notes');

// resultant html
<div class="input textarea">
    <label for="notes">Notes</label>
    <textarea id="notes" rows="5" name="notes"></textarea>
</div>

注意:这在大多数输入类型中是一致的;

Note: this is consistent across most input types; and because it's consistent it's great for formatting.

但是,使用一个复选框:

However, with a checkbox:

//In the view
echo $this->Form->input('ticket_required', 
    ['type' => 'checkbox']
);

// resultant HTML
<div class="input checkbox">
    <input type="hidden" value="0" name="ticket_required">
    <label for="ticket-required">
    <input id="ticket-required" type="checkbox" value="1" name="ticket_required">
    Ticket Required</label>
</div>

[注意:我理解隐藏字段的需要/期望]

[Note: I understand the need/desire for the hidden field]

现在.. 肯定它不是一个罕见的要求,只是想要与其他标准输入相同的格式方法?

Now.. surely it can't be an uncommon requirement to simply want the same format approach as every other standard input?

我的问题 - 我如何使CakePHP创建一个复选框元素如下:

My question - how do I make CakePHP create a checkbox element as follows:

// desired HTML
<div class="input checkbox">
    <input type="hidden" value="0" name="ticket_required">
    <label for="ticket-required">
    Ticket Required</label>
    <input id="ticket-required" type="checkbox" value="1" name="ticket_required">
</div>

要清楚:可见元素的顺序与其他生成的元素和所有封装在包装div)。

To be clear: the order of visible elements is the same as other generated elements (label before input, and all encased in the wrapping div).

请注意..我已经尝试了'nestedInput'=> false 选项。这实际上摆脱了复选框输入完全从div。

Please note.. i have tried the 'nestedInput' => false option. This actually gets rid of the checkbox input entirely from the div.

我不明白为什么这不是这样做的,但即使它是,我不能理解为什么这不是一个明显的问题的文档。

I can't understand why this isn't done that way... but even if it was, I can't fathom why this isn't an obvious question for the documentation.

哦,希望有人可以帮助我在这里。

Oh well.. hopefully someone can help me here.

提前感谢。

Rick

推荐答案

我会认为 nestedInput 可以工作,即使是这样,你不想添加到你在网站上创建的每个输入。

I would have thought the nestedInput would work but even if it did, you don't want to add that to every input you create throughout the website.

CakePHP 3使用字符串模板来构建表单控件。 您可以修改它们以适应您的需要

CakePHP 3 uses string templates to build form controls. You can modify them to suit your needs.

默认情况下,复选框使用 nestingLabel 模板,如果要停止所有输入嵌套您可以更改模板。

By default the checkbox is using the nestingLabel template so if you want to stop all inputs from being nested you can change the template.

// src/View/AppView.php

$this->loadView('Form', [
    templates => [
        'nestingLabel' => '<label{{attrs}}>{{text}}</label>{{hidden}}{{input}}'
    ],
    // [More helper default config overrides][2]..
])

要更好地控制助手,您可以创建您自己的,扩展其中一个核心助手。

For more control over your helpers you can create your own that extends one of the core helpers.

这篇关于CakePHP 3.x复选框格式化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆