Fix: Remove active columns when field removed

This commit is contained in:
philipp lang 2024-03-01 03:00:33 +01:00
parent eaa880127d
commit 3d270ce9a4
2 changed files with 28 additions and 0 deletions

View File

@ -7,6 +7,7 @@ use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Laravel\Scout\Searchable;
use Spatie\Image\Manipulations;
@ -144,6 +145,13 @@ class Form extends Model implements HasMedia
'sorting' => $model->getFields()->count() ? [$model->getFields()->first()['key'], 'asc'] : null,
]);
}
if (is_array(data_get($model->meta, 'active_columns'))) {
$model->setAttribute('meta', [
...$model->meta,
'active_columns' => array_diff($model->getFields()->pluck('key')->toArray(), $model->meta['active_columns']),
]);
}
});
}
}

View File

@ -28,4 +28,24 @@ class FormUpdateActionTest extends FormTestCase
$this->assertTrue(data_get($form->config, 'sections.0.fields.0.max_today'));
}
public function testItUpdatesActiveColumnsWhenFieldRemoved(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
$this->textField('firstname'),
$this->textField('geb'),
$this->textField('lastname'),
])])
->create();
$payload = FormRequest::new()->sections([
FormtemplateSectionRequest::new()->fields([
$this->textField('firstname'),
])
])->create();
$this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk();
$this->assertEquals(['firstname'], $form->fresh()->meta['active_columns']);
}
}