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
First, you need to get an instance of a form.
$form = JForm::getInstance('form_name', JPATH_COMPONENT . '/models/forms/form_name.xml');
Then, you can display or render complete field set.
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"?>
<field name="name" type="text" label="Name" />
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)
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
This is a method to display all control groups with label and input of a fieldset.
5. To Display One Field
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
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
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);
foreach ($fields as $field)
You are not required to change the code even if the names of field set or names of fields changes.
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]" />.
This is a method to remove a field from the form definition. You can remove any field by name before displaying the form.
$form->setFieldAttribute('field_name', 'attribute_name', 'attribute_value');
This is a method to set an attribute value for a field XML element.
$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.