Page 1 of 1

Dynamic form fields

Posted: Mon Jan 21, 2013 9:01 pm
by sohopros
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)?

Re: Dynamic form fields

Posted: Mon Jan 21, 2013 11:58 pm
by ranwilli
And your question is?

Re: Dynamic form fields

Posted: Tue Jan 22, 2013 12:34 am
by sohopros
The question is: How do we accomplish the creation of custom fields on a joomla form xml (fields definition)?

Re: Dynamic form fields

Posted: Wed Jan 23, 2013 7:06 pm
by Tribal6
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

Re: Dynamic form fields

Posted: Wed Jan 30, 2013 11:17 pm
by sohopros
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!

Re: Dynamic form fields

Posted: Wed Jan 30, 2013 11:35 pm
by ranwilli
I'm still looking for a question that makes sense

Re: Dynamic form fields

Posted: Wed Jan 30, 2013 11:40 pm
by sohopros
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!

Re: Dynamic form fields

Posted: Thu Jan 31, 2013 12:11 am
by ranwilli
Nowhere near clear enough.

Re: Dynamic form fields

Posted: Thu Jan 31, 2013 1:56 pm
by sohopros
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.

Re: Dynamic form fields

Posted: Thu Apr 18, 2013 1:56 pm
by riprod
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

Re: Dynamic form fields

Posted: Thu Apr 18, 2013 4:50 pm
by sohopros
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

Re: Dynamic form fields

Posted: Thu Apr 18, 2013 8:36 pm
by Ratmil
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.

Re: Dynamic form fields

Posted: Thu Jul 04, 2013 9:13 pm
by cpegfa
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

Re: Dynamic form fields

Posted: Tue Oct 08, 2013 11:25 pm
by poproar
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.

Re: Dynamic form fields

Posted: Mon Oct 14, 2013 3:13 pm
by oxygen
@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?

Re: Dynamic form fields

Posted: Tue Oct 15, 2013 3:59 pm
by Ratmil
I can do this for you if you want.
PM me.

Re: Dynamic form fields

Posted: Tue Oct 15, 2013 4:02 pm
by Ratmil
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.