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

echo $form->getFieldsets();

Method to get an array of fieldset objects.

2. To Get All Fields in One Field Set

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

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

3. To Display All Fields in One Field Set

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

Method to get all control groups with label and input of a fieldset.

4. To Display One Field of Fieldset

$fields = $form->getFieldset('fieldset_name');
echo $field->renderField('field_name');

Method to get a control group with label and input.

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.

Methods in JFrom

__construct(string $name, array $options = array())

addFieldPath(mixed $new = null) : array

addFormPath(mixed $new = null) : array

addRulePath(mixed $new = null) : array

bind(mixed $data) : boolean

filter(array $data, string $group = null) : mixed

getAttribute(string $name, mixed $default = null) : mixed

getData() : \Joomla\Registry\Registry

getErrors() : array

getField(string $name, string $group = null, mixed $value = null) : \JFormField|boolean

getFieldAttribute(string $name, string $attribute, mixed $default = null, string $group = null) : mixed

getFieldset(string $set = null) : array

getFieldsets(string $group = null) : array

getFieldXml(string $name, string $group = null) : \SimpleXMLElement|boolean

getFormControl() : string

getGroup(string $group, boolean $nested = false) : array

getInput(string $name, string $group = null, mixed $value = null) : string

getInstance(string $name, string $data = null, array $options = array(), boolean $replace = true, string|boolean $xpath = false) : \Joomla\CMS\Form\Form

getLabel(string $name, string $group = null) : string

getName() : string

getValue(string $name, string $group = null, mixed $default = null) : mixed

getXml() : \SimpleXMLElement

load(string $data, string $replace = true, string $xpath = false) : boolean

loadFile(string $file, string $reset = true, string $xpath = false) : boolean

removeField(string $name, string $group = null) : boolean

removeGroup(string $group) : boolean

renderField(string $name, string $group = null, mixed $default = null, array $options = array()) : string

renderFieldset(string $name, array $options = array()) : string

reset(boolean $xml = false) : boolean

setField(\SimpleXMLElement $element, string $group = null, boolean $replace = true, string $fieldset = 'default') : boolean

setFieldAttribute(string $name, string $attribute, mixed $value, string $group = null) : boolean

setFields( &$elements, string $group = null, boolean $replace = true, string $fieldset = 'default') : boolean

setValue(string $name, string $group = null, mixed $value = null) : boolean

validate(array $data, string $group = null) : boolean

Subscribe for Free Updates

Please enable the javascript to submit this form