Repeatable fields prefilled #1215
-
So I have a repeatable field that it will be populated by the user but now asked to get the various items aalready prefilled so he just need to configure some values and not everything. Maybe a solution can be something like in
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Based on the information provided in the knowledge sources, there isn't a direct way to prefill repeatable fields with specific items as you've described. However, I can suggest a potential workaround that might help you achieve a similar result. The repeatable field in Backpack allows you to define a default number of rows or items when the field is first displayed. You can use the [
'name' => 'repeatablefield',
'label' => 'Repeatable Field',
'type' => 'repeatable',
'init_rows' => 3, // This will create 3 empty rows by default
'subfields' => [
// Your subfields here
]
] This approach will create empty rows, but it won't prefill them with specific values. To prefill the repeatable field with specific values, you might need to handle this in your controller. You could set the value of the field before the form is displayed. Something like this: CRUD::field('repeatablefield')->value(json_encode($yourPrefillData)); Where However, it's important to note that this approach is not explicitly documented and might require some experimentation to get right. Another thing to keep in mind is that since Backpack v5, the repeatable field returns an array when the form is submitted, instead of a parsed JSON. You must cast the repeatable field to ARRAY or JSON in your model Backpack Docs. If you need more specific functionality, you might want to consider creating a custom field type that extends the repeatable field, allowing you to add the prefill functionality you need. Remember, when working with repeatable fields, you need to be cautious about using certain field types as subfields, as some may not work correctly within a repeatable context Laravel-Backpack/CRUD#2266. Relevant sources: Github Issue (closed): [Bug] Two different json fields cannot have the same fake fields inside (fake fields with the same name) |
Beta Was this translation helpful? Give feedback.
Based on the information provided in the knowledge sources, there isn't a direct way to prefill repeatable fields with specific items as you've described. However, I can suggest a potential workaround that might help you achieve a similar result.
The repeatable field in Backpack allows you to define a default number of rows or items when the field is first displayed. You can use the
init_rows
attribute to set this. For example:This approach will create empty rows, b…