Add default value for field to Request

This commit is contained in:
philipp lang 2023-12-27 00:42:17 +01:00
parent 6262ccdd28
commit 87ee15936d
3 changed files with 6 additions and 1 deletions

View File

@ -22,6 +22,7 @@ trait HasValidation
'config.sections.*.fields.*.name' => 'required|string',
'config.sections.*.fields.*.type' => ['required', 'string', Rule::in(array_column(Field::asMeta(), 'id'))],
'config.sections.*.fields.*.key' => ['required', 'string', 'regex:/^[a-zA-Z_]*$/'],
'config.sections.*.fields.*.default' => 'present',
'config.sections.*.fields.*.columns' => 'required|array',
'config.sections.*.fields.*.columns.mobile' => 'required|numeric|gt:0|lte:2',
'config.sections.*.fields.*.columns.tablet' => 'required|numeric|gt:0|lte:4',
@ -39,6 +40,7 @@ trait HasValidation
'config.sections.*.fields.*.name' => 'Feldname',
'config.sections.*.fields.*.type' => 'Feldtyp',
'config.sections.*.fields.*.key' => 'Feldkey',
'config.sections.*.fields.*.default' => 'Standardwert',
];
}

View File

@ -13,6 +13,7 @@ use Worksome\RequestFactories\RequestFactory;
* @method self type(string $type)
* @method self rows(int $rows)
* @method self columns(array{mobile: int, tablet: int, desktop: int} $rows)
* @method self default(mixed $default)
*/
class FormtemplateFieldRequest extends RequestFactory
{
@ -26,6 +27,7 @@ class FormtemplateFieldRequest extends RequestFactory
'key' => str($this->faker->words(5, true))->snake()->toString(),
'type' => $this->faker->randomElement(array_column(Field::asMeta(), 'id')),
'columns' => ['mobile' => 2, 'tablet' => 4, 'desktop' => 6],
'default' => '',
];
}

View File

@ -23,7 +23,7 @@ class FormtemplateStoreActionTest extends TestCase
$this->login()->loginNami()->withoutExceptionHandling();
FormtemplateRequest::new()->name('testname')->sections([
FormtemplateSectionRequest::new()->name('Persönliches')->fields([
FormtemplateFieldRequest::new()->type(TextField::class)->name('lala1')->columns(['mobile' => 2, 'tablet' => 2, 'desktop' => 1])->required(false),
FormtemplateFieldRequest::new()->type(TextField::class)->name('lala1')->columns(['mobile' => 2, 'tablet' => 2, 'desktop' => 1])->required(false)->default('zuzu'),
FormtemplateFieldRequest::new()->type(TextareaField::class)->name('lala2')->required(false)->rows(10),
]),
])->fake();
@ -34,6 +34,7 @@ class FormtemplateStoreActionTest extends TestCase
$this->assertEquals('Persönliches', $formtemplate->config['sections'][0]['name']);
$this->assertEquals('lala1', $formtemplate->config['sections'][0]['fields'][0]['name']);
$this->assertEquals('TextField', $formtemplate->config['sections'][0]['fields'][0]['type']);
$this->assertEquals('zuzu', $formtemplate->config['sections'][0]['fields'][0]['default']);
$this->assertEquals('TextareaField', $formtemplate->config['sections'][0]['fields'][1]['type']);
$this->assertEquals(false, $formtemplate->config['sections'][0]['fields'][1]['required']);
$this->assertEquals(['mobile' => 2, 'tablet' => 2, 'desktop' => 1], $formtemplate->config['sections'][0]['fields'][0]['columns']);