Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend examples in guide/input-tabular-input.md #19278

Merged
merged 3 commits into from
Apr 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions docs/guide/input-tabular-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Let's start with the controller action:

namespace app\controllers;

use Yii;
use yii\base\Model;
use yii\web\Controller;
use app\models\Setting;

Expand All @@ -43,11 +41,13 @@ class SettingsController extends Controller
{
$settings = Setting::find()->indexBy('id')->all();

if (Model::loadMultiple($settings, Yii::$app->request->post()) && Model::validateMultiple($settings)) {
foreach ($settings as $setting) {
$setting->save(false);
if ($this->request->isPost) {
if (Setting::loadMultiple($settings, $this->request->post()) && Setting::validateMultiple($settings)) {
foreach ($settings as $setting) {
$setting->save(false);
}
return $this->redirect('index');
}
return $this->redirect('index');
}

return $this->render('update', ['settings' => $settings]);
Expand All @@ -71,10 +71,12 @@ use yii\widgets\ActiveForm;

$form = ActiveForm::begin();

foreach ($settings as $index => $setting) {
echo $form->field($setting, "[$index]value")->label($setting->name);
foreach ($settings as $id => $setting) {
echo $form->field($setting, "[$id]value")->label($setting->name);
}

echo Html::submitButton('Save');

ActiveForm::end();
```

Expand All @@ -88,20 +90,29 @@ Creating new records is similar to updating, except the part, where we instantia
```php
public function actionCreate()
{
$count = count(Yii::$app->request->post('Setting', []));
$settings = [new Setting()];
for($i = 1; $i < $count; $i++) {
$settings[] = new Setting();
$settings = [];
if ($this->request->isPost) {
$count = count($this->request->post($setting->tableName())) - 1;
for ($i = 0; $i < $count; $i++) {
$settings[$i] = new Setting();
}
if (Setting::loadMultiple($settings, $this->request->post()) && Setting::validateMultiple($settings)) {
foreach ($settings as $setting) {
$setting->save(false);
}
return $this->redirect('index');
}
}
$settings[] = new Setting();

// ...
return $this->render('create', ['settings' => $settings]);
}
```

Here we create an initial `$settings` array containing one model by default so that always at least one text field will be
visible in the view. Additionally we add more models for each line of input we may have received.

In the view you can use javascript to add new input lines dynamically.
In the view you can use JavaScript to add new input lines dynamically.

### Combining Update, Create and Delete on one page

Expand Down