Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Forms

A form in Joomla is not laid out by hand. It is described in an XML file: which fields exist, what type they are, how values are cleaned and validated. From this description Joomla renders the fields, checks submitted data and returns the form with error messages.

Where it lives

Component forms are stored in the forms folder of the administrator part — one file per form: administrator/components/com_hello/forms/post.xml. The file is created by hand, and the model loads it by name: $this->loadForm('com_hello.post', 'post', ...).

A single field

<field name="title" type="text" label="Title" />
AttributePurpose
nameThe field name — usually the same as the table column
typeThe field type: it determines what is rendered
labelThe label; a translation key may be used here

The post form

Let us extend the form from the previous lesson:

<?xml version="1.0" encoding="UTF-8"?>
<!-- administrator/components/com_hello/forms/post.xml -->
<form>
    <fieldset name="details">
        <field name="id" type="hidden" />
 
        <field name="title" type="text" label="Title"
               required="true" maxlength="255" filter="string" />
 
        <field name="body" type="editor" label="Text"
               filter="safehtml" buttons="false" />
 
        <field name="published" type="list" label="Status"
               default="1" filter="integer">
            <option value="1">Published</option>
            <option value="0">Hidden</option>
        </field>
    </fieldset>
</form>

Open a post in the panel: the text is now edited with the visual editor, and a Status list appeared below it. The layout did not change — renderFieldset('details') prints all fields of the group.

The editor saves text with HTML tags, and the safehtml filter strips the dangerous ones. So the site layout components/com_hello/tmpl/post/default.php now prints the text without escaping: echo $this->post->body;.

Field types

TypeWhat it renders
text, textarea, hiddenPlain HTML fields
editorThe visual editor
list, radio, checkboxA choice of options
number, email, url, telInputs of the matching type
calendarA date with a calendar
mediaAn image picker from the media manager
userA user picker
categoryA category picker

Filters

A filter cleans the value before validation and saving:

filterWhat remains
stringText without HTML tags
integerAn integer
floatA number with a fractional part
safehtmlHTML without dangerous tags and attributes
rawThe value as is — for trusted data only

Validation

required="true" makes a field mandatory. The validate attribute attaches a validation rule:

<field name="email" type="email" label="Author email" validate="email" />
<field name="site" type="url" label="Website" validate="url" />
<field name="views" type="number" label="Views" validate="number" min="0" max="100000" />

Validation runs on the server when FormController saves the record. Clear the title and press Save: Joomla returns the form with an error message, and the other values you typed stay in the fields. That is exactly why loadFormData() reads data from the session first.

Rendering fields

Call in the layoutWhat it prints
$this->form->renderFieldset('details')All fields of the group
$this->form->renderField('title')One field with its label
$this->form->getLabel('title')Only the label
$this->form->getInput('title')Only the input

Fields are printed one by one when custom markup is needed — for example two columns.

Conditional display

The showon attribute shows a field only for a given value of another field:

<field name="hide_reason" type="text" label="Reason for hiding" showon="published:0" />

The "Reason for hiding" field is visible only when Hidden is selected in the Status list.

Common mistake

A field was added to the form, but its value is not saved. Table writes only the fields that have a table column with the same name. When you add an email field to post.xml, add an email column to the #__hello_posts table as well.

Thanks for reading!

Donate to the site — we work hard for you.