dongyi2083 2019-08-13 07:42
浏览 134
已采纳

在下拉列表中选择True或False应该在mysql表中反映为o和1

I am controlling my c program using this as a web interface, now I added alarm time in which MySQL will get the alarm time and I will use it in my c as input and another one I need to on-off alarm so that need dropdown box in which on-off can be selected but it should reflect as 0 and 1 in MySQL table, so that I can process the value.

I tried to get input and validate it and change it to 0 and 1, as it is yii2 I cannot do that so.

My form page is:

<?= $form->field($model, 'idalarmsnooze')->textInput() ?>

<?= $form->field($model, 'snoozetime')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'alarmflag')->textInput() ?>

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

I need alarm flag as dropdown with true or false and the true or false should reflect as 0 and 1 in MySQL table

  • 写回答

1条回答 默认 最新

  • doufan3958 2019-08-13 08:06
    关注

    You can use the dropDownList()

    So it's

    <?= $form->field($model, 'alarmflag')->dropDownList([1 => 'True', 0 => 'False']) ?>
    

    Add this to your model as an extra safety measure, (If you used the Crud generator, you probably have the rules function, just add the rule)

    public function rules()
    {
        return [
            // add this line
            [['alarmflag'], 'number', 'min' => 0, 'max' => 1],
        ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?