Difference between revisions of "Creating a custom form field type"

From Joomla! Documentation

(DS is not supported in Platform 12.2)
(20 intermediate revisions by 11 users not shown)
Line 1: Line 1:
[[JForm]], a feature introduced in Joomla 1.6, lets you easily create HTML forms (<code><form></code>). Forms created using JForm consist of [[Form field|form fields]], implemented as [[JFormField|JFormFields]]. There is a JFormField for each different field type you can find in a form, such as a text field type and a date field type. JForm supports a large selection of standard field types. For a full list, see [[Standard form field types]].
+
<noinclude><languages /></noinclude>
  
Joomla 1.6 makes it possible to extend standard field types or define your own. For example, if your component manages phone book entries, you might want to define a form field type that outputs a select list of cities. There are several advantages to defining a custom form field type:
+
<noinclude>{{Joomla version|version=3.x|comment=<translate><!--T:1-->
 +
series</translate>}}</noinclude>
 +
{{-}}
 +
{{page|needs review}}
  
 +
<translate>
 +
<!--T:2-->
 +
[[S:MyLanguage/JForm|JForm]], a feature introduced in Joomla! 2.5, lets you easily create HTML forms (<code><form></code>). Forms created using JForm consist of [[S:MyLanguage/Form field|form fields]], implemented as [[S:MyLanguage/JFormField|JFormFields]]. There is a JFormField for each different field type you can find in a form, such as a text field type and a date field type. JForm supports a large selection of standard field types. For a full list, see [[S:MyLanguage/Standard form field types|Standard form field types]].
 +
</translate>
 +
 +
<translate>
 +
<!--T:3-->
 +
Joomla! 2.5 makes it possible to extend standard field types or define your own. For example, if your component manages phone book entries, you might want to define a form field type that outputs a select list of cities. There are several advantages to defining a custom form field type:
 +
</translate>
 +
 +
<translate>
 +
<!--T:4-->
 
* You will be able to mix standard field types with your custom field type in a JForm-based form.
 
* You will be able to mix standard field types with your custom field type in a JForm-based form.
 
* You will eventually have a reusable code package that can be used easily throughout your code.
 
* You will eventually have a reusable code package that can be used easily throughout your code.
 
* Extensions that collaborate with your extension will be able to create form fields without meddling with your database tables and other internals.
 
* Extensions that collaborate with your extension will be able to create form fields without meddling with your database tables and other internals.
 +
</translate>
  
== Form field type class requirements ==
+
<translate>== Form field type class requirements == <!--T:5--></translate>
  
 +
<translate>
 +
<!--T:6-->
 
A form field type is defined in a [[wikipedia:Class (computer programming)|class]] that must be a (not necessarily direct) subclass of JFormField. To work correctly, the class must define at least two methods:
 
A form field type is defined in a [[wikipedia:Class (computer programming)|class]] that must be a (not necessarily direct) subclass of JFormField. To work correctly, the class must define at least two methods:
 +
</translate>
  
 
* <code>public function getLabel()</code>
 
* <code>public function getLabel()</code>
*: This function will be called to create the label that belongs to your field and must return a HTML string containing it. Since JFormField defines a ready-to-use <code>getLabel()</code> implementation, custom form field types usually do not define their own <code>getLabel()</code>. If you leave it out, the inherited method of creating labels will be used. It is recommended to leave out the <code>getLabel()</code> method for consistency and speed unless you actually want to modify the label's HTML.
+
<translate><!--T:7-->
 +
*: This function will be called to create the label that belongs to your field and must return a HTML string containing it. Since JFormField defines a ready-to-use <code>getLabel()</code> implementation, custom form field types usually do not define their own <code>getLabel()</code>. If you leave it out, the inherited method of creating labels will be used. It is recommended to leave out the <code>getLabel()</code> method for consistency and speed unless you actually want to modify the label's HTML.</translate>
 
* <code>public function getInput()</code>
 
* <code>public function getInput()</code>
*: This function will be called to create the field itself and must return a HTML string containing it. This is also where most of the processing usually happens. In our phone book City field example, this function will have to retrieve a list of available cities and return a HTML <code><select></code> with the cities inserted as <code><option></code>s.
+
<translate><!--T:8-->
 +
*: This function will be called to create the field itself and must return a HTML string containing it. This is also where most of the processing usually happens. In our phone book City field example, this function will have to retrieve a list of available cities and return a HTML <code><select></code> with the cities inserted as <code><option></code>s.</translate>
  
 +
<translate>
 +
<!--T:9-->
 
Inside your code, you will have to process the attributes set by the field's user in the XML form definition. Some of those attributes are accessible via protected member variables of JFormField. For example, the <code>name</code> attribute is available in your code as <code>$this->name</code>. Similarly, <code>label</code>, <code>description</code>, <code>default</code>, <code>multiple</code> and <code>class</code> are also available as properties of <code>$this</code>. Other parameters you might have defined can be accessed through the <code>$this->element</code> array: the attribute <code>size</code> will be in <code>$this->element['size']</code>.
 
Inside your code, you will have to process the attributes set by the field's user in the XML form definition. Some of those attributes are accessible via protected member variables of JFormField. For example, the <code>name</code> attribute is available in your code as <code>$this->name</code>. Similarly, <code>label</code>, <code>description</code>, <code>default</code>, <code>multiple</code> and <code>class</code> are also available as properties of <code>$this</code>. Other parameters you might have defined can be accessed through the <code>$this->element</code> array: the attribute <code>size</code> will be in <code>$this->element['size']</code>.
 +
</translate>
  
== Which class to subclass? ==
+
<translate>== Which class to subclass? == <!--T:10--></translate>
  
For a form field type to be usable in JForm, it needs do be a subclass of JFormField. However, it does not have to be a direct child of that class: you can also subclass an existing (standard or custom) form field type and thereby inherit useful code.
+
<translate>
 +
<!--T:11-->
 +
For a form field type to be usable in JForm, it needs to be a subclass of JFormField. However, it does not have to be a direct child of that class: you can also subclass an existing (standard or custom) form field type and thereby inherit useful code.
 +
</translate>
  
'''If your form field type is quite similar to an existing type,''' you should subclass that type. Especially if your form field type is a '''list''', please subclass [[JFormFieldList]]. You only have to override <code>getOptions()</code> method to return the options to be shown; the <code>getInput()</code> method will convert those options to HTML.
+
<translate>
 +
<!--T:12-->
 +
'''If your form field type is quite similar to an existing type,''' you should subclass that type. Especially if your form field type is a '''list''', please subclass [[S:MyLanguage/JFormFieldList|JFormFieldList]]. You only have to override <code>getOptions()</code> method to return the options to be shown; the <code>getInput()</code> method will convert those options to HTML.
 +
</translate>
  
To subclass an existing type, for example JFormFieldList, load it by adding the following to after <code>jimport('joomla.form.formfield');</code>:
+
<translate><!--T:13-->
 +
To subclass an existing type, for example JFormFieldList, load it by adding the following to after <code>jimport('joomla.form.formfield');</code>:</translate>
  
 
<source lang="php">
 
<source lang="php">
Line 31: Line 62:
 
</source>
 
</source>
  
'''If your form field type is unlike any existing type,''' subclass JFormField directly.
+
<translate><!--T:14-->
 +
'''If your form field type is unlike any existing type,''' subclass JFormField directly.</translate>
  
== Location of files ==
+
<translate>== Location of files == <!--T:15--></translate>
  
* The standard form field types are located in <code>joomla/libraries/joomla/form/fields/</code>. You should not store custom fields there, nor should you have to use this path in your own code, but the standard types are usually good examples.
+
<translate><!--T:16-->
* The custom field types that belong to your component are usually located in <code>administrator/components/<name of your component>/models/fields</code>. You can specify this or another path in your code:
+
* The standard form field types are located in <code>libraries/joomla/form/fields/</code>. You should not store custom fields there, nor should you have to use this path in your own code, but the standard types are usually good examples.</translate>
 +
<translate><!--T:17-->
 +
* The custom field types that belong to your component are usually located in <code>administrator/components/<name of your component>/models/fields</code>. You can specify this or another path in your code:</translate>
 
<source lang="php">JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');</source>
 
<source lang="php">JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');</source>
* The XML files that define forms are usually located in <code>administrator/components/<name of your component>/models/forms</code>. Use something like the following snippet to specify a path to your forms:
+
<translate><!--T:18-->
 +
* The XML files that define forms are usually located in <code>administrator/components/<name of your component>/models/forms</code>. Use something like the following snippet to specify a path to your forms:</translate>
 
<source lang="php">JForm::addFormPath(JPATH_COMPONENT . '/models/forms');</source>
 
<source lang="php">JForm::addFormPath(JPATH_COMPONENT . '/models/forms');</source>
  
== Naming conventions and skeleton ==
+
<translate>== Naming conventions and skeleton == <!--T:19--></translate>
  
In this section, <ComponentName> represents the camel-cased name of your component and <TypeName> represents the camel-cased name of your form field type. The field's class should be placed in <code>administrator/components/<name of your component>/models/fields/<name of your field>.php</code>, and look like this:
+
<translate><!--T:20-->
 +
In this section, <ComponentName> represents the camel-cased name of your component and <FieldName> represents the camel-cased name of your form field type. The field's class should be placed in <code>administrator/components/<name of your component>/models/fields/<name of your field>.php</code>, and look like this:</translate>
  
 
<source lang="php"><?php
 
<source lang="php"><?php
Line 67: Line 103:
 
</source>
 
</source>
  
=== Grouping custom field types ===
+
<translate>=== Grouping custom field types === <!--T:21--></translate>
  
'''Warning: this information is partially incorrect and needs to be improved.'''
+
<translate><!--T:22-->
 +
'''Warning: this information is partially incorrect and needs to be improved.'''</translate>
  
Custom field types can be grouped by using an underscore in the field name. A field class with a name for example like "JFormFieldMy_randomField" must be stored in <code>administrator/components/<name of your component>/models/fields/my/randomField.php</code>. We can prefix our form field names with some group name, then we put an underscore and then a name of a field.
+
<translate><!--T:23-->
 +
Custom field types can be grouped by using an underscore in the field name. A field class with a name for example like "JFormFieldMy_randomField" must be stored in <code>administrator/components/<name of your component>/models/fields/my/randomField.php</code>. We can prefix our form field names with some group name, then we put an underscore and then a name of a field.</translate>
  
== An example custom field type ==
+
<translate>== An example custom field type == <!--T:24--></translate>
  
Suppose you're working on your component named ''com_phonebook'' and you want to define a field that contains cities. Create the file <code>administrator/components/com_phonebook/models/fields/city.php</code> and write something similar to the following:
+
<translate><!--T:25-->
 +
Suppose you're working on your component named ''com_phonebook'' and you want to define a field that contains cities. Create the file <code>administrator/components/com_phonebook/models/fields/city.php</code> and write something similar to the following:</translate>
  
 
<source lang="php"><?php
 
<source lang="php"><?php
Line 99: Line 138:
 
</source>
 
</source>
  
=== Using the custom field type ===
+
<translate>=== Using the custom field type === <!--T:26--></translate>
 
+
<translate>==== Linked with a form ==== <!--T:27--></translate>
To use the field type City, we need to update the XML file that contains the form fields. Open your XML file located in <code>administrator/components/com_phonebook/models/forms</code> and add the field in the usual way:
+
<translate><!--T:28-->
 +
To use the field type City, we need to update the XML file that contains the form fields. Open your XML file located in <code>administrator/components/com_phonebook/models/forms</code> and add the field in the usual way:</translate>
  
 
<source lang="xml"><field name="title" type="City" label="JGLOBAL_TITLE"
 
<source lang="xml"><field name="title" type="City" label="JGLOBAL_TITLE"
 
description="JFIELD_TITLE_DESC"
 
description="JFIELD_TITLE_DESC"
 
required="true" /></source>
 
required="true" /></source>
The type name is cAsE-sEnSiTiVe.
 
  
 +
<translate><!--T:29-->
 +
The attribute name is cAsE-sEnSiTiVe.</translate>
  
In addition, you may need to add the field path to the parent <fieldset>:
+
<translate><!--T:30-->
 +
In addition, you may need to add the field path to the parent <fieldset>:</translate>
  
 
<source lang="xml">
 
<source lang="xml">
Line 115: Line 157:
 
</source>
 
</source>
  
== Overriding <code>getLabel()</code> ==
+
<translate>==== Not linked with a form ==== <!--T:31--></translate>
 +
<translate><!--T:32-->
 +
E.g. when you need the field as a dropdown in a component as admin/site filter.</translate>
 +
 
 +
<source lang="php">
 +
//Get custom field
 +
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
 +
$cities = JFormHelper::loadFieldType('City', false);
 +
$cityOptions=$cities->getOptions(); // works only if you set your field getOptions on public!!
 +
</source>
 +
 
 +
<translate>== Overriding <code>getLabel()</code> == <!--T:33--></translate>
  
As mentioned in the section [[#Form field type class requirements|Form field type class requirements]], custom form field types usually do not define their own <code>getLabel()</code>. If you do want to create a custom label, you can still make use of the <code>getLabel()</code> that every field type class inherits from JFormField, for example by defining it as follows:
+
<translate><!--T:34-->
 +
As mentioned in the section [[#Form field type class requirements|Form field type class requirements]], custom form field types usually do not define their own <code>getLabel()</code>. If you do want to create a custom label, you can still make use of the <code>getLabel()</code> that every field type class inherits from JFormField, for example by defining it as follows:</translate>
  
 
<source lang="php">public function getLabel() {
 
<source lang="php">public function getLabel() {
Line 123: Line 177:
 
} </source>
 
} </source>
  
 +
<translate><!--T:35-->
 
This code will underline your form labels. (Please note that if your goal is to underline form labels, using [[CSS]] is the preferred way.)
 
This code will underline your form labels. (Please note that if your goal is to underline form labels, using [[CSS]] is the preferred way.)
  
If you want to do something completely different, you can of course also override it completely:
+
<!--T:36-->
 +
If you want to do something completely different, you can of course also override it completely:</translate>
  
 
<source lang="php">public function getLabel() {
 
<source lang="php">public function getLabel() {
Line 157: Line 213:
 
}</source>
 
}</source>
  
This example will add a checkbox before the label.
+
<translate><!--T:37-->
 +
This example will add a checkbox inside the label.</translate>
  
<noinclude>[[Category:Development]][[Category:Form fields]][[Category:Joomla! 1.6]]</noinclude>
+
<noinclude>
 +
<translate>
 +
<!--T:52-->
 +
[[Category:Development]]
 +
[[Category:Extension development]]
 +
[[Category:Form fields]]
 +
[[Category:Joomla! 2.5]]
 +
[[Category:Joomla! 3.x]]
 +
</translate>
 +
</noinclude>

Revision as of 16:28, 14 September 2015

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français
Joomla! 
3.x
series
Copyedit.png
This Article Needs Your Help

This article is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.


JForm, a feature introduced in Joomla! 2.5, lets you easily create HTML forms (<form>). Forms created using JForm consist of form fields, implemented as JFormFields. There is a JFormField for each different field type you can find in a form, such as a text field type and a date field type. JForm supports a large selection of standard field types. For a full list, see Standard form field types.

Joomla! 2.5 makes it possible to extend standard field types or define your own. For example, if your component manages phone book entries, you might want to define a form field type that outputs a select list of cities. There are several advantages to defining a custom form field type:

  • You will be able to mix standard field types with your custom field type in a JForm-based form.
  • You will eventually have a reusable code package that can be used easily throughout your code.
  • Extensions that collaborate with your extension will be able to create form fields without meddling with your database tables and other internals.

Form field type class requirements[edit]

A form field type is defined in a class that must be a (not necessarily direct) subclass of JFormField. To work correctly, the class must define at least two methods:

  • public function getLabel()
    This function will be called to create the label that belongs to your field and must return a HTML string containing it. Since JFormField defines a ready-to-use getLabel() implementation, custom form field types usually do not define their own getLabel(). If you leave it out, the inherited method of creating labels will be used. It is recommended to leave out the getLabel() method for consistency and speed unless you actually want to modify the label's HTML.
  • public function getInput()
    This function will be called to create the field itself and must return a HTML string containing it. This is also where most of the processing usually happens. In our phone book City field example, this function will have to retrieve a list of available cities and return a HTML <select> with the cities inserted as <option>s.

Inside your code, you will have to process the attributes set by the field's user in the XML form definition. Some of those attributes are accessible via protected member variables of JFormField. For example, the name attribute is available in your code as $this->name. Similarly, label, description, default, multiple and class are also available as properties of $this. Other parameters you might have defined can be accessed through the $this->element array: the attribute size will be in $this->element['size'].

Which class to subclass?[edit]

For a form field type to be usable in JForm, it needs to be a subclass of JFormField. However, it does not have to be a direct child of that class: you can also subclass an existing (standard or custom) form field type and thereby inherit useful code.

If your form field type is quite similar to an existing type, you should subclass that type. Especially if your form field type is a list, please subclass JFormFieldList. You only have to override getOptions() method to return the options to be shown; the getInput() method will convert those options to HTML.

To subclass an existing type, for example JFormFieldList, load it by adding the following to after jimport('joomla.form.formfield');:

jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');

If your form field type is unlike any existing type, subclass JFormField directly.

Location of files[edit]

  • The standard form field types are located in libraries/joomla/form/fields/. You should not store custom fields there, nor should you have to use this path in your own code, but the standard types are usually good examples.
  • The custom field types that belong to your component are usually located in administrator/components/<name of your component>/models/fields. You can specify this or another path in your code:
JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');
  • The XML files that define forms are usually located in administrator/components/<name of your component>/models/forms. Use something like the following snippet to specify a path to your forms:
JForm::addFormPath(JPATH_COMPONENT . '/models/forms');

Naming conventions and skeleton[edit]

In this section, <ComponentName> represents the camel-cased name of your component and <FieldName> represents the camel-cased name of your form field type. The field's class should be placed in administrator/components/<name of your component>/models/fields/<name of your field>.php, and look like this:

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');

// The class name must always be the same as the filename (in camel case)
class JFormField<FieldName> extends JFormField {

	//The field class must know its own type through the variable $type.
	protected $type = '<FieldName>';

	public function getLabel() {
		// code that returns HTML that will be shown as the label
	}

	public function getInput() {
		// code that returns HTML that will be shown as the form field
	}
}

Grouping custom field types[edit]

Warning: this information is partially incorrect and needs to be improved.

Custom field types can be grouped by using an underscore in the field name. A field class with a name for example like "JFormFieldMy_randomField" must be stored in administrator/components/<name of your component>/models/fields/my/randomField.php. We can prefix our form field names with some group name, then we put an underscore and then a name of a field.

An example custom field type[edit]

Suppose you're working on your component named com_phonebook and you want to define a field that contains cities. Create the file administrator/components/com_phonebook/models/fields/city.php and write something similar to the following:

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');

class JFormFieldCity extends JFormField {
	
	protected $type = 'City';

	// getLabel() left out

	public function getInput() {
		return '<select id="'.$this->id.'" name="'.$this->name.'">'.
		       '<option value="1" >New York</option>'.
		       '<option value="2" >Chicago</option>'.
		       '<option value="3" >San Francisco</option>'.
		       '</select>';
	}
}

Using the custom field type[edit]

Linked with a form[edit]

To use the field type City, we need to update the XML file that contains the form fields. Open your XML file located in administrator/components/com_phonebook/models/forms and add the field in the usual way:

<field name="title" type="City" label="JGLOBAL_TITLE"
	description="JFIELD_TITLE_DESC"
	required="true" />

The attribute name is cAsE-sEnSiTiVe.

In addition, you may need to add the field path to the parent <fieldset>:

<fieldset addfieldpath="/administrator/components/<component name>/models/fields">

Not linked with a form[edit]

E.g. when you need the field as a dropdown in a component as admin/site filter.

//Get custom field
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
$cities = JFormHelper::loadFieldType('City', false);
$cityOptions=$cities->getOptions(); // works only if you set your field getOptions on public!!

Overriding getLabel()[edit]

As mentioned in the section Form field type class requirements, custom form field types usually do not define their own getLabel(). If you do want to create a custom label, you can still make use of the getLabel() that every field type class inherits from JFormField, for example by defining it as follows:

public function getLabel() {
     return '<span style="text-decoration: underline;">' . parent::getLabel() . '</span>';
}

This code will underline your form labels. (Please note that if your goal is to underline form labels, using CSS is the preferred way.)

If you want to do something completely different, you can of course also override it completely:

public function getLabel() {
	// Initialize variables.
	$label = '';
	$replace = '';

	// Get the label text from the XML element, defaulting to the element name.
	$text = $this->element['label'] ? (string) $this->element['label'] : (string) $this->element['name'];

	// Build the class for the label.
	$class = !empty($this->description) ? 'hasTip' : '';
	$class = $this->required == true ? $class.' required' : $class;
		
	// Add replace checkbox
	$replace = '<input type="checkbox" name="update['.$this->name.']" value="1" />';
		
	// Add the opening label tag and main attributes attributes.
	$label .= '<label id="'.$this->id.'-lbl" for="'.$this->id.'" class="'.$class.'"';

	// If a description is specified, use it to build a tooltip.
	if (!empty($this->description)) {
		$label .= ' title="'.htmlspecialchars(trim(JText::_($text), ':').'::' .
				JText::_($this->description), ENT_COMPAT, 'UTF-8').'"';
	}

	// Add the label text and closing tag.
	$label .= '>'.$replace.JText::_($text).'</label>';
	
	return $label; 
}

This example will add a checkbox inside the label.