API16

Difference between revisions of "JForm/getFieldset"

From Joomla! Documentation

< API16:JForm
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
===Syntax===
 
===Syntax===
<source lang="php">getFieldset()</source>
+
<source lang="php">getFieldset($fieldset)</source>
  
 
===Defined in===
 
===Defined in===
Line 47: Line 47:
  
 
===Examples===
 
===Examples===
<CodeExamplesForm />
+
 
<dpl>
 
noresultsheader=\n
 
category=getFieldsets
 
category=JForm
 
category=CodeExample
 
category=MethodExample
 
include=*
 
format= ,,,
 
</dpl>
 
 
For this example, consider a situation in which we're building an MVC component. The following is a list of the files and directories inside our "com_mvc" folder:
 
For this example, consider a situation in which we're building an MVC component. The following is a list of the files and directories inside our "com_mvc" folder:
  
Line 71: Line 62:
 
</source>
 
</source>
  
We'll assume for this example that we have also have a controller which is instantiating our "mvcview", which is a class that extends JView. The following are the contents of the view.html.php file:
+
We'll assume for this example that we have also have a controller which is instantiating our "mvcViewmvc", which is a class that extends JView. The following is the content of the view.html.php file:
  
 
<source lang="php">
 
<source lang="php">
Line 118: Line 109:
 
         public function getForm($data = array(), $loadData = true)  
 
         public function getForm($data = array(), $loadData = true)  
 
         {
 
         {
                 $form = $this->loadForm('com_mvc.mvc', 'form', array('control' => 'jform', 'load_data' => $loadData);
+
                 $form = $this->loadForm(
 +
                                        'com_mvc.mvc',
 +
                                        'form',
 +
                                        array('control' => 'jform', 'load_data' => $loadData
 +
                                      );
 
                 return $form;
 
                 return $form;
 
         }
 
         }
Line 182: Line 177:
 
This template will display only the fields which we have defined in our form.xml file that are within the "primary_fieldset." This becomes very handy if you have fieldsets that you want to display from different templates, but they all fall under the purview of a single data model.
 
This template will display only the fields which we have defined in our form.xml file that are within the "primary_fieldset." This becomes very handy if you have fieldsets that you want to display from different templates, but they all fall under the purview of a single data model.
  
 +
<CodeExamplesForm />
 
<dpl>
 
<dpl>
 
  noresultsheader=\n
 
  noresultsheader=\n
  category=getFieldsets
+
  category=getFieldset
 
  category=JForm
 
  category=JForm
 
  category=CodeExample
 
  category=CodeExample

Revision as of 05:08, 27 August 2010

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Syntax[edit]

getFieldset($fieldset)

Defined in[edit]

libraries/joomla/form/form.php

Importing[edit]

jimport( 'joomla.form.form' );

Source Body[edit]

public function getFieldset($set = null)
{
	// Initialize variables.
	$fields = array();

	// Get all of the field elements in the fieldset.
	if ($set) {
		$elements = $this->findFieldsByFieldset($set);
	}
	// Get all fields.
	else {
		$elements = $this->findFieldsByGroup();
	}

	// If no field elements were found return empty.
	if (empty($elements)) {
		return $fields;
	}

	// Build the result array from the found field elements.
	foreach ($elements as $element) {

		// Get the field groups for the element.
		$attrs	= $element->xpath('ancestor::fields[@name]/@name');
		$groups	= array_map('strval', $attrs ? $attrs : array());
		$group	= implode('.', $groups);

		// If the field is successfully loaded add it to the result array.
		if ($field = $this->loadField($element, $group)) {
			$fields[$field->id] = $field;
		}
	}

	return $fields;
}


Examples[edit]

For this example, consider a situation in which we're building an MVC component. The following is a list of the files and directories inside our "com_mvc" folder:

models/
models/mvcmodel.php
models/forms/
models/forms/form.xml
views/
views/mvc/
views/mvc/view.html.php
views/mvc/tmpl/
views/mvc/tmpl/default.php

We'll assume for this example that we have also have a controller which is instantiating our "mvcViewmvc", which is a class that extends JView. The following is the content of the view.html.php file:

class mvcViewmvc extends JView
{
        /**
         * View form
         *
         * @var         form
         */         
        protected $form = null;
        /**
         * display method of MVC view
         * @return void
         */
        public function display($tpl = null) 
        {
                // get the Form
                $form = & $this->get('Form');
                // get the Data
                if (count($errors = $this->get('Errors'))) 
                {
                        JError::raiseError(500, implode("<br />", $errors));
                        return;
                }
                // Assign the form
                $this->form = $form;
                // Display the template
                parent::display();
        }
}

This view uses the mvcmodelmvc data model to get the form data, and the default template to display the form. Looking at the code in our mvcmodel.php file will shed some light on how the form.xml data is loaded:

class mvcModelmvc extends JModelAdmin
{

        /**
         * Method to get the form.
         *
         * @access      public
         * @return      mixed   JForm object on success, false on failure.
         */
        public function getForm($data = array(), $loadData = true) 
        {
                $form = $this->loadForm(
                                        'com_mvc.mvc',
                                        'form',
                                        array('control' => 'jform', 'load_data' => $loadData
                                       );
                return $form;
        }
}

We can see that the getForm method defined in our data model calls the loadForm method, which will load the data from the form.xml file. Let's take a look inside our form.xml file:

<?xml version="1.0" encoding="utf-8"?>
<form>
        <fieldset name="primary_fieldset">
                <field
                        id="id"
                        name="id"
                        type="hidden"
                />
                <field
                	id="primary"
                        name="primary"
                        type="list"
                        label="Primary Selection"
                        default="red">
                        <option value="red">Red</option>
                        <option value="blue">Blue</option>
                </field>
        </fieldset>
        <fieldset name="secondary_fieldset">
                <field
                        id="id"
                        name="id"
                        type="hidden"
                />
                <field
                	id="secondary"
                        name="secondary"
                        type="list"
                        label="Secondary Selection"
                        default="green">
                        <option value="green">Green</option>
                        <option value="yellow">Yellow</option>
                </field>
        </fieldset>
</form>

So, as of right now, we've called the default template and passed $this to it. $this has our form contained in the form variable, accessible through $this->form. Since $this->form is a JForm object, we can call getFieldset($fieldset) from it. Consider the default.php template file:

<div class="first-fieldset">
    <form name="formname">
        <fieldset class="adminform">
            <legend>Primary Fields</legend>
            <?php foreach ($this->form->getFieldset('primary_fieldset') as $field): ?>
                <?php echo $field->label; ?>
                <?php echo $field->input; ?>
            <?php endforeach; ?>
        </fieldset>
    </form>
</div>

This template will display only the fields which we have defined in our form.xml file that are within the "primary_fieldset." This becomes very handy if you have fieldsets that you want to display from different templates, but they all fall under the purview of a single data model.

<CodeExamplesForm />