[英]Yii2: How to get checkox's value from controller
I need to get the value of a checkbox called Password using the Yii2 Framework, in the controller. 我需要在控制器中使用Yii2 Framework获取一个名为Password的复选框的值。
In my _form.php I define the checkbox: 在我的_form.php中我定义了复选框:
<?= Html::checkbox('password', false, $options = ['label' => 'Reset password']) ?>
In my UserController.php I have the actionUpdate function: 在我的UserController.php中,我有actionUpdate函数:
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
echo '<pre>';
echo Yii::$app->request->post()['password'];
echo '</pre>';
return $this->render('update', [
'model' => $model,
]);
}
}
I always get the value 1 instead of true or false . 我总是得到值1而不是真或假 。
First of all, there is error in your code, it should be: 首先,代码中存在错误,应该是:
<?= Html::checkbox('password', false, ['label' => 'Reset password']) ?>
This checkbox generates 1
when checked by default. 默认情况下选中此复选框时会生成1
。 If you want to send 0
when checkbox is not selected you must add: 如果要在未选中复选框时发送0
,则必须添加:
<?= Html::checkbox('password', false, ['label' => 'Reset password', 'uncheck' => 0]) ?>
Checkbox return 1 for true(checked) and return 0 for false(not checked). 复选框返回1表示true(选中),返回0表示false(未选中)。
You can check in controller as below 您可以按照以下方式登记控制器
if(isset($model->password) && $model->password =="1") //check box is checked
{
// code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.