MVC model - correct way to add fields at runtime?

259 views
Skip to first unread message

AJH

unread,
Jul 29, 2012, 11:10:43 PM7/29/12
to joomla-de...@googlegroups.com
I'm having difficulty injecting new fields to a model's form at runtime.  I have a separate configuration area for field definitions, and I need to add those fields to a model's for at run time.

So far, I have overridden the preprocessForm method of my model, and have kluged together a proof that the field will appear in the view if create a new SimpleXMLElement, and call $form->setField(...) with that xml definition.

What sees more natural is to instantiate the correct JFormField class, and attach it to the form object, but I don't seem to see any API to do so.

Any suggestions?

KHarmer

unread,
Aug 2, 2012, 6:52:24 PM8/2/12
to joomla-de...@googlegroups.com
I'm intrigued, but confused about what you're asking. Can you explain a bit more?

K...

AJH

unread,
Aug 3, 2012, 2:15:08 AM8/3/12
to joomla-de...@googlegroups.com
I apologize for the somewhat incoherent question I originally posted.  It was late :-)

My component allows for new fields to be defined by the end-user for any object that supports it within the component.

I accomplished this by subclassing JModelAdmin, and making all of my models inherit from my new base admin model.  Within this class, I override the 'preprocessForm' method that is called by the MVC framework and add the custom fields into the form.  It's working great, except I'm curious if there is a better way of doing it.

Here's a snippet from that method. This block of code is looping over all custom fields that are configured for the object.  What seems a bit kludgy is how I have to create an XML representation of the fields and then shove them into the form with the ->setField(..) call.  It seems that you should be able to use the JField objects and add them to the form.

// this defines how the field will be rendered.
$fieldType = 'inputbox';
$fieldOptions = '';
$fieldFormat = '';
switch ($field->typecode)
{
case 'BIT':
$fieldType = 'radio';
$fieldOptions = '<option value="0">JNO</option><option value="1">JYES</option>';
break;
case 'TEXT':
$fieldType = 'textarea';
break;
case 'DATE':
$fieldType = 'calendar';
break;
case 'DATETIME':
$fieldType = 'calendar';
$fieldFormat = 'format="%Y-%m-%d %H:%M:%S"';
break;
}
$xml = sprintf('<fieldset name="udf"><field type="%s" name="udfId%d" label="%s" description="%s" %s>%s</field></fieldset>'
, $fieldType
, $field->id
, htmlspecialchars($field->code)
, htmlspecialchars($field->description)
, $fieldFormat
, $fieldOptions);
// this assigns the stored UDF data into the data that is bound to the form
$data->{'udfId'.$field->id} = $field->data;
$xmlElement = new SimpleXMLElement($xml);
$form->setField($xmlElement);

Adam Rifat

unread,
Aug 3, 2012, 1:48:04 PM8/3/12
to joomla-de...@googlegroups.com
Hi,

For what its worth I find myself doing the same thing on a component I am developing.

It would be nice if there was an api for this but it doesn't seem there is one.

I wonder how easy it would be to add...or if its planned?

Ads
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

AJH <aaron...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/44IvBNM3YcEJ.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
Reply all
Reply to author
Forward
0 new messages