Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
MVC model - correct way to add fields at runtime?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
AJH  
View profile  
 More options Jul 29 2012, 11:10 pm
From: AJH <aaron.hu...@gmail.com>
Date: Sun, 29 Jul 2012 20:10:43 -0700 (PDT)
Local: Sun, Jul 29 2012 11:10 pm
Subject: MVC model - correct way to add fields at runtime?

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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
KHarmer  
View profile  
 More options Aug 2 2012, 6:52 pm
From: KHarmer <kristianhar...@gmail.com>
Date: Thu, 2 Aug 2012 15:52:24 -0700 (PDT)
Local: Thurs, Aug 2 2012 6:52 pm
Subject: Re: MVC model - correct way to add fields at runtime?

I'm intrigued, but confused about what you're asking. Can you explain a bit
more?

K...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
AJH  
View profile  
 More options Aug 3 2012, 2:15 am
From: AJH <aaron.hu...@gmail.com>
Date: Thu, 2 Aug 2012 23:15:08 -0700 (PDT)
Local: Fri, Aug 3 2012 2:15 am
Subject: Re: MVC model - correct way to add fields at runtime?

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);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Rifat  
View profile  
 More options Aug 3 2012, 1:48 pm
From: Adam Rifat <a...@littledonkey.net>
Date: Fri, 03 Aug 2012 18:48:04 +0100
Local: Fri, Aug 3 2012 1:48 pm
Subject: Re: [jgen] Re: MVC model - correct way to add fields at runtime?

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.hu...@gmail.com> wrote:

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);

On Thursday, 2 August 2012 15:52:24 UTC-7, KHarmer wrote:

I'm intrigued, but confused about what you're asking. Can you explain a bit more?

K...

--
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-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »