T
T
tursumbekov2016-02-29 08:40:34
MySQL
tursumbekov, 2016-02-29 08:40:34

Yii2: checkbox values ​​not being sent?

There are checkboxes on yii2 created via ActiveForm:

<?= $form->field($model, 'agency_full', [
            'template' => '{input}{label}',
            'options' => [
                'class' => 'checkbox-group'
            ]
        ])->checkbox(['class' => 'checkbox'], false)->label( 'Агентство полного цикла', ['class' => 'for-checkbox']); ?>
        <?= $form->field($model, 'design_studio', [
            'template' => '{input}{label}',
            'options' => [
                'class' => 'checkbox-group'
            ]
        ])->checkbox(['class' => 'checkbox'], false)->label( 'Дизайн-студия', ['class' => 'for-checkbox']); ?>

Generate code like this:
<div class="checkbox-group field-signupform-agency_full">
<input type="hidden" name="SignupForm[agency_full]" value="0">
<input type="checkbox" id="signupform-agency_full" class="checkbox" name="SignupForm[agency_full]" value="1">
<label class="for-checkbox" for="signupform-agency_full">Агентство полного цикла</label>
</div>

Fields in MySQL:
9b4b004a0a16466ebb8c22857b43f7f2.png
So, when submitting the form, regardless of whether the checkboxes are checked or not, the NULL value comes. Although it should come 0 or 1. What could be the problem?
Model:
class SignupForm extends Model
{
    ...
    public $agency_full;
    public $design_studio;
    ...
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }
        
        $user = new User();
        ...
        $user->agency_full = $this->agency_full;
        $user->design_studio = $this->design_studio;
        ...
        return $user->save() ? $user : null;
    }
}

There are no rules for checkboxes.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korovin, 2016-02-29
@tursumbekov

show rules

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question