Actions

API16

Difference between revisions of "JForm/getFieldset"

From Joomla! Documentation

< API16:JForm
(Examples)
(Examples)
Line 62: 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">

Revision as of 04:04, 27 August 2010

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

Syntax

getFieldset()

Defined in

libraries/joomla/form/form.php

Importing

jimport( 'joomla.form.form' );

Source Body

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

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 />