简体   繁体   English

在Phalcon中使用复选框创建表单

[英]Creating Form with checkboxes in Phalcon

I'm building a form which reads a list of keywords from a database. 我正在建立一个从数据库中读取关键字列表的表单。 The model is simple: each user have different keywords associated in the database, so I don't know the number of them. 该模型很简单:每个用户在数据库中都有不同的关键字,因此我不知道它们的数量。

In the form I want to render all the user's associated keywords with a checkbox list, so the user can decide which keyword save in a special group. 在表单中,我要使用复选框列表呈现所有与用户相关的关键字,以便用户可以决定将哪个关键字保存在特殊组中。 Of course I want to render the name of the keyword but I want to obtain the "id" of it. 当然,我想呈现关键字的名称,但我想获取它的“ id”。

I don't find any documentation of this. 我没有找到任何相关文档。 I just found the typical: 我刚刚发现了典型的:

$keywords = new Check('keywords', array(
        'value' => '1'
    ));
$keywords->setLabel('Keywords');
$this->add($keywords); 

to put in the form, but it is useless. 放在表格中,但这是没有用的。 In the view I wrote 我认为

<div class="control-group">
        {{ form.label('keywords', ['class': 'control-label']) }}
        <div class="controls">
            {{ form.render('keywords', ['class': 'form-control']) }}
        </div>
</div>

And I see a checkbox (with value 1). 我看到一个复选框(值为1)。 I imagine the solution should be something like the SELECT (which I use in another form). 我想解决方案应该是类似SELECT(我以另一种形式使用)的东西。 Something like: 就像是:

$idkeyword = new Select('keyword',
        Keyword::find($string), [
        "useEmpty"  =>  true,
        "emptyText" =>  "Por favor selecciona...", 
        "using" => ["idkeyword", "trackeable"],
        ]);
$idkeyword->setLabel('Keyword');
$idkeyword->addValidators(array(
        new PresenceOf(array(
            'message' => 'idkeyword requerida'
            ))
        ));
$this->add($idkeyword);

In the view I would like to have something like: 在视图中,我希望有以下内容:

<input type="checkbox" name="chk_group" value="1" />Keyword 1<br />
<input type="checkbox" name="chk_group" value="2" />Keyword 2<br />
<input type="checkbox" name="chk_group" value="3" />Keyword 3<br />

When "Keyword X" is in the database and "X" is its id. 当“关键字X”在数据库中且“ X”为其ID时。

I would be glad to hear any help. 我很高兴听到任何帮助。 I hope my question is well formulated. 我希望我的问题得到很好的阐述。 If not, I will accept all comments. 如果没有,我将接受所有评论。 Thanks. 谢谢。

The Phalcon\\Forms\\Element\\Check is meant for a single checkbox, so if you want to use it for multiple checkboxes, you will have to write a loop: Phalcon\\Forms\\Element\\Check用于单个复选框,因此,如果要将其用于多个复选框,则必须编写一个循环:

// You should get these options from the database
$options = [1 => 'Keyword 1', 2 => 'Keyword 2', 3 => 'Keyword 3'];

foreach($options as $key => $value)
{
    // Create a checkbox for each option
    $keywords = new Check('keywords'.$key, [
        'name'  => 'chk_group',
        'class' => 'form-control',
        'value' => $value
    ]);

    // Create a label for each option so the user can click on this
    $keywords->setLabel($value);

    $this->add($keywords);
}

then in the view: 然后在视图中:

{% for element in form %}
    {{ element.label(['class': 'control-label']) }}
    {{ element.render() }}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM