Working With Joomla Forms Using JForm Class

Form fields are fields in a HTML <form>. In Joomla, you can use JForm class to conveniently and flexibly create forms with a large amount of form fields. These form fields are stored in XML file.

The XML file is generally stored in the folder: models/forms/form_name.xml

Step 1

First, you need to get an instance of a form.

$form = JForm::getInstance('form_name', JPATH_COMPONENT . '/models/forms/form_name.xml');

Step 2

Then, you can display or render complete field set.

echo $form->renderFieldset('fieldset_name');

How To Create XML Form: form_name.xml

Following is the example of XML form. You can add more fields in the fieldset or create new fieldset.

<?xml version="1.0" encoding="utf-8"?>
<form class="form-validate">
<fieldset name="fieldset_name">
<field name="name" type="text" label="Name" />
</fieldset>
</form>

This XML file is generally placed in the folder: models/forms/form_name.xml

How To Display Form

The XML form can have many field sets. Each field set can contain many fields.

1. To Get All Fields in All Field Sets

$fieldsets = $form->getFieldsets();

This is a method to get an array of fieldset objects.

2. To Get All Fields in One Field Set

$fields = $form->getFieldset('fieldset_name');

This is a method to get an array of fields objects in a given fieldset by name. If no name is given, then all the fields are returned.

Now, to display input or label or display control group with label and input:

foreach($fields as $field)
{
echo $field->input;
echo $field->label;
echo $field->renderField();
}

You can also access field attributes like $field->name, $field->label, etc.

3. To Get One Field

$field = $form->getField('field_name');

This is a method to get a form field represented as a JFormField object.

4. To Display All Fields in One Field Set

echo $form->renderFieldset('fieldset_name');

This is a method to display all control groups with label and input of a fieldset.

5. To Display One Field

echo $form->renderField('field_name');

This is a method to display a control group (<div> tag) with label (<label> tag) and input of any one field by name.

6. To Display Only Input of One Field

echo $form->getInput('field_name');

This is a method to get a form field markup for the field input. It will not display the control group and label.

7. To Display Only Label of One Field

echo $form->getLabel('field_name');

This is a method to get the label for a field input.

8. To Get Attribute of One Field

echo $form->getFieldAttribute('field_name', 'attribute_name');

This is a method to get an attribute value from a field XML element. If the attribute doesn't exist or is null, then the optional default value will be used. Attribute names can be name, type, label, default, class, hint, etc.

Display Form Using Loop

You can display multiple field sets using loops.

foreach ($this->form->getFieldsets() as $fieldset)
{
$fields = $this->form->getFieldset($fieldset->name);
if (count($fields))
{
foreach ($fields as $field)
{
echo $field->renderField();
}
}
}

You are not required to change the code even if the names of field set or names of fields changes.

Other Methods

1. getFormControl

This is a method to get the form control. This string serves as a container (array) for all form fields. For example, if there is a field named 'foo' and a field named 'bar' and the form control is empty, then the fields will be rendered like: <input name="foo" /> and <input name="bar" />. If the form control is set to 'joomla', then the fields would be rendered like: <input name="joomla[foo]" /> and <input name="joomla[bar]" />.

echo $form->getFormControl();

2. removeField

$form->removeField('field_name');

This is a method to remove a field from the form definition. You can remove any field by name before displaying the form.

3. setFieldAttribute

$form->setFieldAttribute('field_name', 'attribute_name', 'attribute_value');

This is a method to set an attribute value for a field XML element.

4. setValue

$form->setValue('field_name', NULL, 'field_value');

This is a method to set the value of a field. If the field does not exist in the form, then the method will return false.

5. setField

First, define the field element.

$element = '<field name="title" type="text" label="Title" />';

$xml = new SimpleXMLElement($element);

$form->setField($xml, $group = null, $replace = true, $fieldset = 'default');

This method sets a field XML element to the form definition. If the replace flag is set, then the field will be set whether it already exists or not. If it isn't set, then the field will not be replaced if it already exists.