【问题标题】:Phalcon - multiple checkboxes in formsPhalcon - 表单中的多个复选框
【发布时间】:2013-11-01 13:58:08
【问题描述】:

基本上,我需要在将多个复选框保存到数据库之前对其进行序列化,并在显示表单之前对其进行反序列化。

<input type="checkbox" name="list[option1]" value="1">
<input type="checkbox" name="list[option2]" value="1">
<input type="checkbox" name="list[option3]" value="1">

有人能指出我正确的方向吗?

我已尝试使用以下代码生成复选框,但在请求后无法正常工作。 所选选项未填充到表单中(其他字段很好)

<?php
$form->bind($_POST, $entity);
....
foreach ($list as $key => $option) {
  $form->add(new Check("list[$key]", array('value' => 1)));
}

我想使用多选框也存在同样的问题。

【问题讨论】:

    标签: php phalcon


    【解决方案1】:

    我想你有一个错字。试试:

    <?php
    $form->bind($_POST, $entity);
    ....
    foreach ($list as $key => $option) {
      $form->add(new Check($list[$key], array('value' => 1)));
    }
    

    附带说明,Phalcon\Tag 帮助器可用于生成 HTML。

    <?php
    
    echo Phalcon\Tag::checkField(array($list[$key], "value" => "1"));
    

    【讨论】:

    • 这不是错字。我故意需要将选定的复选框作为 POST 数组发送
    【解决方案2】:

    您可以使用我的代码 Fdola.com。 使用

    <?php 
    $list_module = new \App\Vendor\Fdola\Forms\CheckBoxList('list_module', ['a' => 'A', 'b' => 'B'], ['a'], ['class' => 'checkBoxList']);
    $list_module->setLabel('Module hiển thị banner:');
    $list_module->addValidators([
        new \Phalcon\Validation\Validator\PresenceOf([
            'message' => '<b>:field</b> không được phép rỗng'
        ])
    ]);
    $this->add($list_module);
    

    <?php
    /**
     * Created by PhpStorm.
     * User: thanhansoft
     * Date: 4/29/2016
     * Time: 4:21 PM
     */
    
    namespace App\Vendor\Fdola\Forms;
    
    use Phalcon\Http\Request;
    use Phalcon\Tag;
    
    class CheckBoxList extends \Phalcon\Forms\Element {
        private $_data;
        private $_dataOld;
    
        public function __construct($name, $data, $dataOld = null, $attribute = null) {
            $this->_data = $data;
            $this->_dataOld = $dataOld;
            parent::__construct($name, $attribute);
        }
    
        public function render($attribute = null) {
            $get_value = $this->getValue();
            if ($get_value) {
                $data = $get_value;
            } else {
                $data = $this->_dataOld;
            }
    
            $tag = new Tag();
            $string = '';
            if ($this->_data) {
                foreach ($this->_data as $key => $value) {
                    $arr = ['id' => $this->_name . '-' . $key, 'name' => $this->_name . '[]', 'value' => $key];
    
                    if ($data && in_array($key, $data)) {
                        $arr['checked'] = 'checked';
                    }
    
                    $string .= '<label>' . $tag::checkField($arr) . ' ' . $value . '</label>';
                }
            }
    
            if (isset($this->_attributes['class'])) return '<div class="' . $this->_attributes['class'] . '">' . $string . '</div>';
            return $string;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-28
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2019-02-03
      • 2012-10-06
      • 2020-04-28
      • 1970-01-01
      相关资源
      最近更新 更多