【问题标题】:Creating Form with checkboxes in Phalcon在 Phalcon 中使用复选框创建表单
【发布时间】:2017-05-06 06:43:35
【问题描述】:

我正在构建一个从数据库中读取关键字列表的表单。 模型很简单:每个用户在数据库中关联的关键字不同,所以不知道有多少个。

在表单中,我想用复选框列表呈现所有用户关联的关键字,以便用户可以决定将哪个关键字保存在特殊组中。 当然我想渲染关键字的名称,但我想获取它的“id”。

我没有找到这方面的任何文档。我刚刚找到了典型的:

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

把表格放进去,但没用。在我写的视图中

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

我看到一个复选框(值为 1)。我想解决方案应该类似于 SELECT(我以另一种形式使用)。比如:

$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);

在视图中我想要类似的东西:

<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 />

当“Keyword X”在数据库中并且“X”是它的id时。

我很乐意听到任何帮助。我希望我的问题表述得很好。如果没有,我会接受所有的 cmets。谢谢。

【问题讨论】:

    标签: php forms checkbox phalcon


    【解决方案1】:

    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);
    }
    

    然后在视图中:

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

    【讨论】:

      猜你喜欢
      • 2015-11-28
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 2016-02-29
      • 2015-07-20
      • 1970-01-01
      相关资源
      最近更新 更多