Dynamic form fields

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
User avatar
sohopros
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 106
Joined: Fri Jul 22, 2011 1:51 pm
Contact:

Dynamic form fields

Post by sohopros » Mon Jan 21, 2013 9:01 pm

We are creating a Joomla component and using xml to define most of the form fields on backend. But we have one form that needs dynamic fields.
The input field is dynamic, the category that shows on top, and the label for the field will all come from the database.
The values will be stored in a table in a key-value relationship. Please, check the attached mockup for better understanding.

How do we accomplish the creation of custom fields on a joomla form xml (fields definition)?
You do not have the required permissions to view the files attached to this post.
Last edited by sohopros on Tue Jan 22, 2013 12:35 am, edited 1 time in total.
SOHO Prospecting
https://www.sohoprospecting.com - Joomla Website development
Southern California - USA
Phone 866.644.7646

User avatar
ranwilli
Joomla! Master
Joomla! Master
Posts: 19203
Joined: Sun Feb 19, 2006 6:47 pm
Location: Toledo, OH
Contact:

Re: Dynamic form fields

Post by ranwilli » Mon Jan 21, 2013 11:58 pm

And your question is?
Don't HACK the Joomla! core, Instead "Extend" and/or "Override."
Stay ON the update path.
https://harpervance.com

User avatar
sohopros
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 106
Joined: Fri Jul 22, 2011 1:51 pm
Contact:

Re: Dynamic form fields

Post by sohopros » Tue Jan 22, 2013 12:34 am

The question is: How do we accomplish the creation of custom fields on a joomla form xml (fields definition)?
SOHO Prospecting
https://www.sohoprospecting.com - Joomla Website development
Southern California - USA
Phone 866.644.7646

Tribal6
Joomla! Apprentice
Joomla! Apprentice
Posts: 22
Joined: Fri Apr 07, 2006 7:36 pm

Re: Dynamic form fields

Post by Tribal6 » Wed Jan 23, 2013 7:06 pm

Hi

Make a folder somwhere in your component called 'fields'. Inside this folder copy one of a core JForm field from ../libraries/joomla/form/fields (i.e. for dropdowns you can take list.php). Rename it (File, Classname, $type) to your custom fieldname, and customise the 'getOptions'-method (you could place a sql-query there, to 'ask' your DB about options).

Then, in your form.xml write somthing like that

Code: Select all

<fieldset addfieldpath="/administrator/components/com_mycomponent/path/to/fields/folder" >
If everthing is done, you should be able to use your file in that .xml like

Code: Select all

<field name="yourfieldname" 
    type="yourcostomfield" ....../>
Attributes and childelements depends on what you have implemented in your field-class.

If once you have done that, you be able to use that field in front- or backend, or in other components to.

Hope it helps
Cheers
Roger
...sorry, but I'm just here to learn english!

User avatar
sohopros
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 106
Joined: Fri Jul 22, 2011 1:51 pm
Contact:

Re: Dynamic form fields

Post by sohopros » Wed Jan 30, 2013 11:17 pm

Tribal6, thank you for your help but this is not really what we are trying to accomplish.
Please, check the attached mockup (from our first post) for better understanding.

We must have form like that

Dynamic Category A:
Dynamic Field A label: Dynamic Field A input
Dynamic Field B label: Dynamic Field B input

Dynamic Category B:
Dynamic Field A label: Dynamic Field A input
Dynamic Field B label: Dynamic Field B input

Not options of the fields but fields are coming from database.


We are still looking for an answer.
Thanks!
SOHO Prospecting
https://www.sohoprospecting.com - Joomla Website development
Southern California - USA
Phone 866.644.7646

User avatar
ranwilli
Joomla! Master
Joomla! Master
Posts: 19203
Joined: Sun Feb 19, 2006 6:47 pm
Location: Toledo, OH
Contact:

Re: Dynamic form fields

Post by ranwilli » Wed Jan 30, 2013 11:35 pm

I'm still looking for a question that makes sense
Don't HACK the Joomla! core, Instead "Extend" and/or "Override."
Stay ON the update path.
https://harpervance.com

User avatar
sohopros
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 106
Joined: Fri Jul 22, 2011 1:51 pm
Contact:

Re: Dynamic form fields

Post by sohopros » Wed Jan 30, 2013 11:40 pm

ranwilli, thank you for your reply.
How do we accomplish this using Joomla 2.5 form xml through the <field />?

Please let us know if we clear enough in our explanation.

Thanks!
SOHO Prospecting
https://www.sohoprospecting.com - Joomla Website development
Southern California - USA
Phone 866.644.7646

User avatar
ranwilli
Joomla! Master
Joomla! Master
Posts: 19203
Joined: Sun Feb 19, 2006 6:47 pm
Location: Toledo, OH
Contact:

Re: Dynamic form fields

Post by ranwilli » Thu Jan 31, 2013 12:11 am

Nowhere near clear enough.
Don't HACK the Joomla! core, Instead "Extend" and/or "Override."
Stay ON the update path.
https://harpervance.com

User avatar
sohopros
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 106
Joined: Fri Jul 22, 2011 1:51 pm
Contact:

Re: Dynamic form fields

Post by sohopros » Thu Jan 31, 2013 1:56 pm

We want fields on the form that come from the database, so that when a field item is added to the component (like adding a Zone D on the attached screenshot), that new field will show up on our add/edit form (mock-up for that form is attached to our original posting).

The Zones in this case are our referenced dynamic form fields.
You do not have the required permissions to view the files attached to this post.
SOHO Prospecting
https://www.sohoprospecting.com - Joomla Website development
Southern California - USA
Phone 866.644.7646

riprod
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Sun May 23, 2010 11:25 pm

Re: Dynamic form fields

Post by riprod » Thu Apr 18, 2013 1:56 pm

Seems perfectly clear to me....

You want to have dynamic options (drawn from the database) in the drop down list but you can't put PHP code in the forms XML, so how do call the records from the database and reference them in the XML as :
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

I'm trying to do the same thing. If you found an answer, please post it. Thanks

User avatar
sohopros
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 106
Joined: Fri Jul 22, 2011 1:51 pm
Contact:

Re: Dynamic form fields

Post by sohopros » Thu Apr 18, 2013 4:50 pm

Hi riprod!

In our case, we had to create a custom form field plugin to accomplish what we needed. But, I believe that to generate a dynamic dropdown loading the options from database you can use a SQL type field. Please, check the links bellow:

//Creating a custom form field type
http://docs.joomla.org/Creating_a_custo ... field_type

//SQL form field type
http://docs.joomla.org/SQL_form_field_type

//Standard form field types
http://docs.joomla.org/Form_field

Hope it helps!
SOHO Prospecting Team
SOHO Prospecting
https://www.sohoprospecting.com - Joomla Website development
Southern California - USA
Phone 866.644.7646

User avatar
Ratmil
Joomla! Explorer
Joomla! Explorer
Posts: 318
Joined: Sat Dec 06, 2008 7:07 pm

Re: Dynamic form fields

Post by Ratmil » Thu Apr 18, 2013 8:36 pm

I understand what you say.
You would have to create a custom field that is able to support input for different fields (that must be retrived from database).
I guess you could do that. That is, a custom field that shows different input controls, depending on the configuration on database.
The hard would be putting the values in a single field (maybe a hidden field) so it can be stored correctly in database.

cpegfa
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue Jun 11, 2013 6:44 am

Re: Dynamic form fields

Post by cpegfa » Thu Jul 04, 2013 9:13 pm

Look for the language field as a reference (/libraries/joomla/form/fields/language.php) which generate the dynamic list of languages as options of a pulldown menu based on the database. It is calling a helper function (/libraries/joomla/language/helper.php).

Oliver

User avatar
poproar
Joomla! Intern
Joomla! Intern
Posts: 89
Joined: Fri May 07, 2010 4:43 am
Location: SoCal
Contact:

Re: Dynamic form fields

Post by poproar » Tue Oct 08, 2013 11:25 pm

sohopros wrote:Hi riprod!

In our case, we had to create a custom form field plugin to accomplish what we needed. But, I believe that to generate a dynamic dropdown loading the options from database you can use a SQL type field. Please, check the links bellow:

//Creating a custom form field type
http://docs.joomla.org/Creating_a_custo ... field_type

//SQL form field type
http://docs.joomla.org/SQL_form_field_type

//Standard form field types
http://docs.joomla.org/Form_field

Hope it helps!
SOHO Prospecting Team
sohopros,

would be great if you could share an example of how you executed this.
I am attempting something similar and the js in the view is getting in my way.

oxygen
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Thu May 14, 2009 10:00 am

Re: Dynamic form fields

Post by oxygen » Mon Oct 14, 2013 3:13 pm

@sohopros - did you find a way to do this? I'm trying to achieve the same thing and I can't figure a way of doing it using the Joomla! "xml/fields" method.

For those who don't understand the question (@ranwilli - why bother being that nonconstructive?) as far as I understand what sohopros is trying to do (and me) is:

Create a component in the admin area of which it is possible to create form fields to use elsewhere (front-end in my case) within the component. These fields will be stored in the database.
As these fields will be named "dynamically" as they are created in the component, you cannot (seemingly) use the normal Joomla! method for creating the "edit.php" views for displaying/editing.

Example (based on MY requirements):

OK for a "fixed" form field:

administrator/components/my_component/models/forms/release_credit.xml

Code: Select all

<field name="num_credits" type="numfields" default="" 
            label="Number of credits available to this user" class="readonly" readonly="true"
            description="How many credits available?	"  />
administrator/components/my_component/views/release_credit/tmpl/edit.php

Code: Select all

<div class="control-group">
	<div class="control-label"><?php echo $this->form->getLabel('num_credits'); ?></div>
	<div class="controls"><?php echo $this->form->getInput('num_credits'); ?></div>
	</div>
So, along with the field type file we can display the form field.

But if I want to use this method for many fields that are created dynamically in the admin area and front-end users fill them in to populate a db table I can't use the method I describe above as I don't know what the fields will be!

OK, I could create my own forms in the view and override the "save" function but I'd like to do it 'properly' if there is such a way to do it.

My current line of thinking is to populate the xml file programmaticaly each time a new form field is added, but this seems like a right old faff to me.

Anyone got any further ideas?

User avatar
Ratmil
Joomla! Explorer
Joomla! Explorer
Posts: 318
Joined: Sat Dec 06, 2008 7:07 pm

Re: Dynamic form fields

Post by Ratmil » Tue Oct 15, 2013 3:59 pm

I can do this for you if you want.
PM me.

User avatar
Ratmil
Joomla! Explorer
Joomla! Explorer
Posts: 318
Joined: Sat Dec 06, 2008 7:07 pm

Re: Dynamic form fields

Post by Ratmil » Tue Oct 15, 2013 4:02 pm

Anyway, what´s wrong in overwritting the save method?
This would be in order to put all field values into one value.
Otherwise you would have to create a hidden field and write the value using javascript.


Locked

Return to “Joomla! 2.5 Coding”