dyoungers wrote:
Is there a good way to define the fields dynamically? I need to be able to let the user define the fields that are stored so I can't build an XML file ...
Thanks!
Dave
To do this, to my mind the most secure and robust way of doing this would be to create a table that stores field types, then allow administrators of your component to add fields within the confines of the defined field types you give them.
So let's say for example you wanted to give users the option to create their own select list/combo field and a radio field:
- Create a table 'MyComponentName Custom Fields' and add an enumeration field
- Add the 'Select' & 'Radio' options to the field
- Add fields to the table that represent the parameters of the combo field that you want to allow admins to override (such as 'field_id', 'field_label', 'field_class' etc, etc...)
Then, depending on whether it is one or multiple fields you want to allow them to add to the form:
- Either:
- (Case Single) - Add an FK field to the table you want to add custom field data to, allowing you to get custom field definitions.
- (Case Mulitple) - Add another 'join' table that stores the FKs of both the item you wish to add custom fields to + the custom field id FK. Then, naturally, you'll need to get a 'filtered' view of records from the custom fields table where the item id (form you're adding them to) == the FK.
- Clone & Override the base JDom radio & combo inputs to allow values from your custom fields table to be used as args in your JDom calls
Of course, you'll have to get your hands a bit dirty in the code but, this shouldn't be too difficult.
Following these basic principles, you should be able to render your custom fields.
Further to this, if these custom fields are likely to be used throughout the rest of your component, i.e. not just in one view, you might consider writing an event or helper function (example:
onGetMycomponentCustomFields() ) that you can call throughout your component.
Then, you can return custom fields wherever you like.
Hope it helps!
Best of luck,
Gez