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

在下拉列表中选择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

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],
    ];
}