M
M
Maxim2016-05-17 12:42:21
Yii
Maxim, 2016-05-17 12:42:21

Why does the name not get into name?

There is a model form.php
public $date;
And

public static function checkFormData()
    {
      $allNameEx = exercise::getAllExercises();

      foreach($allNameEx as $item)
      {
        $nameEx[] = $item['name'];
      }
        
        return $nameEx;
    }

variable return $nameEx; returns:
Array
(
    [0] => Sport
    [1] => Reading
    [2] => Paint
    [3] => Walk
)

In views/index
I am creating a form
<?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'date')->widget(yii\jui\DatePicker::className(),['dateFormat' => 'yyyy-MM-dd'])?>

    <?= $form->field($model, 'name')->checkboxList(form::checkFormData()); ?>

    <div class="form-group">
        <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
    </div>

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

I create a form with checkboxes and checkbox names as in an array of
Type Sport, Reading, etc.
<div id="form-name"><label><input type="checkbox" name="form[name][]" value="0"> Sport</label>
<label><input type="checkbox" name="form[name][]" value="1"> Reading</label>
<label><input type="checkbox" name="form[name][]" value="2"> Paint</label>
<label><input type="checkbox" name="form[name][]" value="3"> Walk</label></div>

In the lable, the names are drawn from the array, but in name="form[name][]" they do not fall, why?
And how can I get access to check whether the checkbox is checked or not in the controller?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-05-17
@KidsBout

And they shouldn't go there. The keys of the array fall into the value of the input. This is quite enough. You can change
to
but I would do this:

public static function checkFormData()
    {
      $allNameEx = exercise::getAllExercises();
       return ArrayHelper::map($allNameEx, 'name', 'name');
    }

The effect is the same but shorter, now you will have Sport in value, etc.
By the way, be sure to read: www.yiiframework.com/doc-2.0/guide-helper-array.ht...
Iterate over incoming form[name]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question