getting issue in repeater while generating dynamic field

this is my repeater field
Repeater::make('fields')
->schema([
Select::make('field_type')
->options(FieldTypeEnums::getTypes())
->afterStateUpdated(function ($set, $state) {
if ($state === 'select' || $state === 'radio') {
$set('field_options', []);
}
})
->reactive()
->required(),
Grid::make()
->schema(function (Closure $get) use ($form) {
$field_type = $get('field_type');
$optional_fields = self::getOptionalFieldsFormSchema($field_type, $form);
return $optional_fields;
})->columnSpan(2),
Toggle::make('field_is_required')
->onIcon('heroicon-s-check')
->offIcon('heroicon-s-x')
->required()
->inline(),
])
Repeater::make('fields')
->schema([
Select::make('field_type')
->options(FieldTypeEnums::getTypes())
->afterStateUpdated(function ($set, $state) {
if ($state === 'select' || $state === 'radio') {
$set('field_options', []);
}
})
->reactive()
->required(),
Grid::make()
->schema(function (Closure $get) use ($form) {
$field_type = $get('field_type');
$optional_fields = self::getOptionalFieldsFormSchema($field_type, $form);
return $optional_fields;
})->columnSpan(2),
Toggle::make('field_is_required')
->onIcon('heroicon-s-check')
->offIcon('heroicon-s-x')
->required()
->inline(),
])
I have a select field where users can choose a field type. Based on the selected field type, the form dynamically generates different types of fields. The code successfully generates the fields, but there is an issue when I choose a specific field type (e.g., text field), enter some data into the generated text field, and then choose a different field type from the select field. In this case, the data I entered in the text field before changing the field type is still retained. This issue occurs when the fields are located inside a repeater the code works fine if fields are outside repeater
3 Replies
ba_mbi_07
ba_mbi_07OP17mo ago
this is how i am generating dynamic field
public static function getOptionalFieldsFormSchema($field_type)
{
if (empty($field_type)) {
return [];
}

$createTextInput = function($field) {
return TextInput::make('field_default_value')
->id($field)
->label(strval(__('cranberry-muffin::cranberry-muffin.form.default-value')))
->numeric(fn (Closure $get) => $get('field_type') === 'number');
};
$createTagsInput = function($field) {
return TagsInput::make('field_options')
->id($field)
->label(strval(__('cranberry-muffin::cranberry-muffin.form.field-options')))
->placeholder(strval(__('cranberry-muffin::cranberry-muffin.form.field-options.placeholder')));
};


$fieldHandlers = [
'text' => $createTextInput,
'text_area' => $createTextInput,
'number' => $createTextInput,
'select' => $createTagsInput,
'radio' => $createTagsInput,
];

$schema = [];

if (isset($fieldHandlers[$field_type])) {
$handler = $fieldHandlers[$field_type];
$schema[] = $handler($field_type);
}
return $schema;
}
public static function getOptionalFieldsFormSchema($field_type)
{
if (empty($field_type)) {
return [];
}

$createTextInput = function($field) {
return TextInput::make('field_default_value')
->id($field)
->label(strval(__('cranberry-muffin::cranberry-muffin.form.default-value')))
->numeric(fn (Closure $get) => $get('field_type') === 'number');
};
$createTagsInput = function($field) {
return TagsInput::make('field_options')
->id($field)
->label(strval(__('cranberry-muffin::cranberry-muffin.form.field-options')))
->placeholder(strval(__('cranberry-muffin::cranberry-muffin.form.field-options.placeholder')));
};


$fieldHandlers = [
'text' => $createTextInput,
'text_area' => $createTextInput,
'number' => $createTextInput,
'select' => $createTagsInput,
'radio' => $createTagsInput,
];

$schema = [];

if (isset($fieldHandlers[$field_type])) {
$handler = $fieldHandlers[$field_type];
$schema[] = $handler($field_type);
}
return $schema;
}
toeknee
toeknee17mo ago
You need unique names.
ba_mbi_07
ba_mbi_07OP17mo ago
yes they have unique my text field name is field_default_value and tag input field name is field_options
Want results from more Discord servers?
Add your server