Actions

API16

JModelForm/getForm

From Joomla! Documentation

< API16:JModelForm
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Method to get a form object.

[Edit Descripton]

Description:JModelForm/getForm

Syntax

getForm($xml, $name= 'form', $options=array(), $clear=false)
Parameter Name Default Value Description
$xml $xml The form data. Can be XML string if file flag is set to false.
$name 'form' $options Optional array of parameters.
$options array() $clear Optional argument to force load a new form.
$clear false

Returns

mixed object on success, False on error.

Defined in

libraries/joomla/application/component/modelform.php

Importing

jimport( 'joomla.application.component.modelform' );

Source Body

function getForm($xml, $name = 'form', $options = array(), $clear = false)
{
        // Handle the optional arguments.
        $options['array']       = array_key_exists('array',     $options) ? $options['array'] : false;
        $options['file']        = array_key_exists('file',      $options) ? $options['file']  : true;
        $options['event']       = array_key_exists('event',     $options) ? $options['event'] : null;
        $options['group']       = array_key_exists('group',     $options) ? $options['group'] : null;
 
        // Create a signature hash.
        $hash = md5($xml.serialize($options));
 
        // Check if we can use a previously loaded form.
        if (isset($this->_forms[$hash]) && !$clear) {
                return $this->_forms[$hash];
        }
 
        // Get the form.
        jimport('joomla.form.form');
        JForm::addFormPath(JPATH_COMPONENT.DS.'models'.DS.'forms');
        JForm::addFieldPath(JPATH_COMPONENT.'/models/fields');
        $form = &JForm::getInstance($xml, $name, $options['file'], $options);
 
        // Check for an error.
        if (JError::isError($form))
        {
                $this->setError($form->getMessage());
                $false = false;
                return $form;
        }
 
        // Look for an event to fire.
        if ($options['event'] !== null)
        {
                // Get the dispatcher.
                $dispatcher     = &JDispatcher::getInstance();
 
                // Load an optional plugin group.
                if ($options['group'] !== null) {
                        JPluginHelper::importPlugin($options['group']);
                }
 
                // Trigger the form preparation event.
                $results = $dispatcher->trigger($options['event'], array($form->getName(), $form));
 
                // Check for errors encountered while preparing the form.
                if (count($results) && in_array(false, $results, true))
                {
                        // Get the last error.
                        $error = $dispatcher->getError();
 
                        // Convert to a JException if necessary.
                        if (!JError::isError($error)) {
                                $error = new JException($error, 500);
                        }
 
                        return $error;
                }
        }
 
        // Store the form for later.
        $this->_forms[$hash] = $form;
 
        return $form;
}

[Edit See Also] SeeAlso:JModelForm/getForm

Examples

<CodeExamplesForm />