php Yii 2-我如何设置复选框被选中并默认保存?

rnmwe5a2  于 2023-06-20  发布在  PHP
关注(0)|答案(1)|浏览(119)

大家好,我有复选框的问题,我必须设置默认情况下被选中,其他人编程这一点,我是新的这段代码,所以如果有人知道如何解决这个问题,我将不胜感激。
下面是我的代码:

use app\settings\SettingsModel;
use app\helpers\Html;
use yii\helpers\ArrayHelper;
use youdate\widgets\ActiveForm;
use trntv\aceeditor\AceEditor;

/** @var $elements array */
/** @var $title string */
/** @var $subTitle string */
/** @var $model SettingsModel */

$message = \Yii::$app->session->getFlash('settings');
?>

<div class="card">
    <div class="card-header">
        <h3 class="card-title"><?= Html::encode($title) ?></h3>
    </div>
    <?php $form = ActiveForm::begin() ?>
    <div class="card-body">
        <?= $this->render('/_alert') ?>

        <?php if (isset($subTitle)): ?>
            <h4 class="mb-5"><?= Html::encode($subTitle) ?></h4>
        <?php endif; ?>

        <?php foreach ($elements as $alias => $element) {
            if (is_callable($element['type'])) {
                echo $element['type']($model, $alias);
            } else {
                switch ($element['type']) {
                    case 'dropdown':
                        echo $form->field($model, $alias)
                            ->dropDownList(is_callable($element['options']) ? $element['options']() : $element['options'], [
                                'prompt' => Yii::t('youdate', '-- Select --'),
                            ])
                            ->hint($element['help']);
                        break;
                    case 'checkboxList':
                        echo $form->field($model, $alias)->checkboxList($element['options'])->hint($element['help']);
                        break;
                    case 'checkbox':
                        echo $form->field($model, $alias)->checkbox(isset($element['options']) ?: [])->hint($element['help']);
                        break;
                    case 'code':
                        echo $form->field($model, $alias)->widget(AceEditor::class, ArrayHelper::merge(isset($element['options']) ? $element['options'] : [], [
                            'mode' => 'php',
                            'containerOptions' => [
                                'style' => 'width: 100%; min-height: 200px'
                            ]
                        ]))->hint($element['help']);;
                        break;
                    default:
                    case 'text':
                        echo $form->field($model, $alias)->hint($element['help']);;
                }
            }
        } ?>
    </div>
    <div class="card-footer d-flex justify-content-end">
        <?= Html::submitButton(Yii::t('youdate', 'Save'), ['class' => 'btn btn-primary']) ?>
    </div>
    <?php $form->end() ?>
</div>```

I tried to use something like this :

$model->$alias= true; echo $form->field($model,$alias)->checkbox(isset($element['options'])?:[])->hint($element['help']);

But this code only change checkboxes visualy it's not saved like that...
When checkboxes all really saved and checked cron job script is runned but in this case cron job can not do anything. Hope you guys understand what i want...
wwwo4jvm

wwwo4jvm1#

按照你的代码:

  • )->checkbox(isset($element['options']) ?: [])选项通过$element['options'],其中一个数组元素可以是'checked' => 1
  • 也可以硬编码)->checkbox(['checked' => 1])
  • 即使是基于条件,比如说,你的字段的名称,你可以合并$element['options']和你的硬编码['checked' => 1],检查将被覆盖。
  • 一个选项也在case 'checkbox':块内,设置$model->$alias = true

相关问题