简体   繁体   English

Yii2 CheckboxList 不回馈价值

[英]Yii2 CheckboxList doesn't give back value

I try to make a checkboxlist field where the user select numbers and the selected values are saved to the database .我尝试创建一个复选框列表字段,用户选择数字并将所选值保存到数据库中。

I'm strugling with this problem a few day and i'm alread tryed everything我在这个问题上挣扎了几天,我已经尝试了一切

So here is my code -its already mixed with codes from other posts- Oh and the checkboxlist doesn't give back the value just an empty array Form:所以这是我的代码 - 它已经与来自其他帖子的代码混合 - 哦,复选框列表不返回值只是一个空数组表单:


    <?php $list = ["1" => '1', "2" => '2', "3" => '3', "4" => '4'];?>

    <?php $form = ActiveForm::begin(); ?>
    <?= $form->field($jegyModel, 'chairline') ?>
    <?= $form->field($jegyModel, 'name') ?>
    <?= $form->field($jegyModel, 'tnumber') ?>
    <?= $form->field($jegyModel, 'email') ?>
    <?= $form->field($takModel, 'quantArray')->checkboxList($list,['inline'=>true])?>
    <div class="form-group">
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>



<?=print_r($takModel->quantArray); give back empty array

?>
<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'movie',
        'startday',
        'start',
        'end',
        [
            'value' => $model->ticketprice,
            'label' => 'Ticketprice',
        ],
    ],
]) ?>

And i have a class based on a post what i found here我有一个基于我在这里找到的帖子的课程

<?php
namespace app\models;
class TakMolForm extends Sjegyek
{
    private $_quantArray;

    public function getQuantArray()
    {
        if($this->_quantArray == null)
        {
            $this->_quantArray = explode(',', $this->chairnumber);
        }
        return $this->_quantArray;
    }

    public function setQuantArray($value)
    {
        $this->_quantArray = $value;
    }


    public function rules()
    {
        return array_merge(parent::rules(), [
            [['quantArray'], 'safe'],
        ]);
    }
}

controller控制器

 public function actionFilm($id)
    {
        $jegyModel = new Sjegyek();
        $takModel = new TakMolForm();



        if ($jegyModel->load(Yii::$app->request->post())) {
            $jegyModel->chairnumber = implode(',', $takModel->quantArray);
            $jegyModel->movieid=$id;
            if($jegyModel->save()) {
                return $this->render('test', [
                    'model' => $this->findModel($id),
                    'jegyModel' => $jegyModel,
                    'takModel' => $takModel,
                ]);

            } else {
                return $this->render('film', [
                    'model' => $this->findModel($id),
                    'jegyModel' => $jegyModel,
                    'takModel' => $takModel,

                ]);

            }

        }
        return $this->render('film', [
            'model' => $this->findModel($id),
            'jegyModel' => $jegyModel,
            'takModel' => $takModel,
        ]);

    }

and model for Sjegyek和 Sjegyek 的模型

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "jegyek".
 *
 * @property string $chairnumber
 * @property string $chairline
 * @property string $name
 * @property string $tnumber
 * @property string $email
 * @property int $movieid
 */
class Sjegyek extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'jegyek';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {

        return [
            [['chairnumber', 'chairline', 'name', 'tnumber', 'email', 'movieid'], 'required'],
            [['movieid'], 'integer'],
            [['chairnumber', 'name', 'email'], 'string', 'max' => 255],
            [['chairline'], 'string', 'max' => 4],
            [['tnumber'], 'string', 'max' => 250],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'chairnumber' => 'Chairnumber',
            'chairline' => 'Chairline',
            'name' => 'Name',
            'tnumber' => 'Tnumber',
            'email' => 'Email',
            'movieid' => 'Movieid',
        ];
    }
}

This post is my last hope because im already try everything what i saw here .这篇文章是我最后的希望,因为我已经尝试了我在这里看到的一切。

您可以将 ichsanmust\\grid 用于清单列

    if (Yii::$app->request->post()) {
      $jegyModel->load(Yii::$app->request->post());
      $takModel->load(Yii::$app->request->post());
    
      rest of your code
    }

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

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