Viszman
Viszman

Reputation: 1398

dynamicly add form fields in joomla

I'm wondering if there is a possibility to add form fields to depending on some options,

for example:

when user select number of options = 8 and user click save, on next view user see 8 text box each with different names.

I was trying to use form fields class not via .xml files but can't get it to work.

Any1 know if it is even possible?

Upvotes: 0

Views: 1694

Answers (2)

ironbone
ironbone

Reputation: 101

To be able to add the list items dynamically you have to write your own JFormField like class and install in inside your component.

In your case you extend the JFormFieldList and override the getOptions() function. In .xml you use the new created input type name.

Here how to create and use new fields classes (start here): http://docs.joomla.org/Creating_a_custom_form_field_type

Next the example how to extend the JFormFieldList class: http://docs.joomla.org/J2.5:How_to_add_custom_filters_to_components

In the second link for simplicity use (for example only)

public function getOptions()
    {
            // Initialize variables.
            $options = array(0=>"Option 0", 1=>"Option 1");


            return $options;
    }

Upvotes: 1

Techie
Techie

Reputation: 45124

Yes that's is possible. You can get the number of fields from the form. Assume It's 10($count).

Next view you can loop as below and create text boxes.

for($i = 1; $i <= $count; $i++)
 echo '<input type="text" name="fields[]" class="class_fields">';

At the backend you can fetch the data as below.

$values = JRequest::getVar( 'fields', 'default_values', 'post', 'array' );

Read more

If you have any issues let me know

Upvotes: 1

Related Questions