Matt Hampel
Matt Hampel

Reputation: 5257

Saving a checkbox value in Yii

I can't figure out how to properly save checkbox values in Yii. I have a MySQL column, active, defined as a tinyint. I have the following form creation code, which correctly shows the checkbox as checked if the value is 1 and unchecked if 0:

    <?php echo $form->labelEx($model,'active'); ?>
    <?php echo $form->checkBox($model,'active'); ?>
    <?php echo $form->error($model,'active'); ?>

And the code to save the form correctly changes other, text-based values:

public function actionUpdate($id)
{
    $model=$this->loadModel($id);

    if(isset($_POST['Thing']))
    {
        $model->attributes=$_POST['Thing'];
        if($model->save())
            $this->redirect(array('thing/index'));
    }

    $this->render('update',array(
        'model'=>$model,
    ));
}

The value of active is not saved. Where am I going wrong?

Upvotes: 16

Views: 46127

Answers (10)

Ammar Dje
Ammar Dje

Reputation: 90

well this post is so old but I've found a solution very useful specially for giving checkbox a value specified rather than number. The new syntax is something like this notice I'm using ActiveForm

field($model3, 'External_Catering')->checkbox(['id' => 'remember-me-ver', 'custom' => true,'value'=>"External_Catering", 'uncheckValue'=>"vide"]) ?>

1) where my model is =>model3

2) with the name External_Catering

3) that take the value External_Catering and empty when it's uncheckValue

4) in Controller you get the value just by specifying the model and it's attribute like   $External_Catering=$model3->External_Catering.

Upvotes: 0

Mahendran Sakkarai
Mahendran Sakkarai

Reputation: 8549

We can also add a rule as safe in model to pass the values from form to controller without missing.

array('active', 'safe'),

Upvotes: 0

trai bui
trai bui

Reputation: 598

I have similar the same problemce before,I change data type is int,so it save

Upvotes: 0

To&#241;o
To&#241;o

Reputation: 11

I used a bit type field in my DB and it didn't work.

1.- I changed the field type to tinyint
2.- In the rules function added:
    array('active','numerical'),
3.-In the form (as D3K said) do: 
    <?echo $form->checkBox($model,'active',array('value'=>1, 'uncheckValue'=>0));?> 

Upvotes: 1

Dhiraj Pandey
Dhiraj Pandey

Reputation: 181

You can check by printing all the attributes which are being captured. If active is not captured, it must not be safe. you need to declare the variable as safe or define a rule around that variable. This will make the variable safe.

Upvotes: 0

Php Sofsol
Php Sofsol

Reputation: 71

Please follow: 1. in protected/models/Thing.php add active as a numeric

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('active', 'numerical', 'integerOnly'=>true),
            //OR optional 
            array('active', 'safe'),
    );
}

Controller action: Its ok

View:

<?php echo $form->labelEx($model,'active'); ?>
<?php echo $form->checkBox($model,'active', array('value'=>1, 'uncheckValue'=>0)); ?>
<?php echo $form->error($model,'active'); ?>

Hope this will work for you...

Upvotes: 6

Alexei Tenitski
Alexei Tenitski

Reputation: 9370

Article which can be helpful when figuring out how to handle booleans & checkboxes in Yii

http://www.larryullman.com/2010/07/25/handling-checkboxes-in-yii-with-non-boolean-values/

Upvotes: 2

D3 K
D3 K

Reputation: 682

You can use htmlOptions array to specify value attribute. Below is the code example:

<?php echo $form->labelEx($model,'active'); ?>
<?php echo $form->checkBox($model,'active', array('value'=>1, 'uncheckValue'=>0)); ?>
<?php echo $form->error($model,'active'); ?>

Since version 1.0.2, a special option named 'uncheckValue' is available that can be used to specify the value returned when the checkbox is not checked. By default, this value is '0'. (This text is taken from YII Documenration)

Upvotes: 20

Dafeng
Dafeng

Reputation: 118

For every input that you are accepting from user, you need to define it in model::rule(). is active defined there in rule()?

Upvotes: 17

Neil McGuigan
Neil McGuigan

Reputation: 48267

In general, if you are having problems saving to the database, i would replace

$model->save();

with

if($model->save() == false) var_dump($model->errors);

that way, you can see exactly why it did not save. it is usually a validation error.

Upvotes: 6

Related Questions