Skip to content

Commit 0fae351

Browse files
committed
fixes #7
1 parent 0b3194a commit 0fae351

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

scripts/steps/step-001.3.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,30 @@ public function rules()
8989
Только вместо массива будем использовать запрос `Star::find()->all()`, который вернёт массив моделей.
9090

9191
```php
92+
use common\models\Star;
93+
9294
$stars = [];
9395

9496
foreach (Star::find()->all() as $star){
9597
$stars[$star->id] = $star->name;
9698
}
9799

98-
echo $form->field($model, 'planet')->dropDownList($stars);
100+
echo $form->field($model, 'planet')->dropDownList(
101+
$stars,
102+
['prompt' => 'Выберите звезду'] // текст, который отображается в качестве первого варианта
103+
);
104+
99105
```
100106

101107
Но можно переписать этот код с использованием класса помощника <a href="http://www.yiiframework.com/doc-2.0/yii-helpers-arrayhelper.html" target="_blank">
102108
yii\helpers\ArrayHelper</a>, который позволяет обращаться с массивами более эффективно:
103109

104110
```php
111+
use yii\helpers\ArrayHelper;
112+
use common\models\Star;
113+
105114
$stars = ArrayHelper::map(Star::find()->all(), 'id', 'name');
106-
echo $form->field($model, 'planet')->dropDownList($stars);
115+
echo $form->field($model, 'planet')->dropDownList($stars, ['prompt' => 'Выберите звезду']);
107116
```
108117

109118
Конечно, вы можете обойтись без переменной `$stars`, записав этот код одну строку. Ну и после всего, для этой формы
@@ -113,6 +122,8 @@ echo $form->field($model, 'planet')->dropDownList($stars);
113122
<?php $form = ActiveForm::begin(['layout' => 'horizontal',]); ?>
114123
```
115124

125+
Только не забудьте, что в этом случае нужно использовать `yii\bootstrap\ActiveForm` вместо `yii\widgets\ActiveForm`.
126+
116127
<img src="/scripts/assets/screen1.2-4.jpg" class="img-responsive">
117128

118129
Осталось <a href="/yii2-app-advanced/backend/web/index.php?r=satellite/create" target="_blank">форма создания спутников</a>.

0 commit comments

Comments
 (0)