Actions

API16

JForm/bind

From Joomla! Documentation

< API16:JForm
Revision as of 20:52, 13 May 2013 by JoomlaWikiBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 recursively bind values to form fields.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

bind($data, $limit=null)
Parameter Name Default Value Description
$data $data An array or object of form values.
$limit null $group The group to bind the fields to.

Returns

boolean True on success, false otherwise.

Defined in

libraries/joomla/form/form.php

Importing

jimport( 'joomla.form.form' );

Source Body

public function bind($data, $limit = null)
{
        // The data must be an object or array.
        if (!is_object($data) && !is_array($data)) {
                return false;
        }
 
        // Convert objects to arrays.
        if (is_object($data)) {
                // Handle a JRegistry/JParameter object.
                if ($data instanceof JRegistry) {
                        $data = $data->toArray();
                }
                // Handle a JObject.
                elseif ($data instanceof JObject) {
                        $data = $data->getProperties();
                }
                // Handle other types of objects.
                else {
                        $data = (array)$data;
                }
        }
 
        // Iterate through the groups.
        foreach ($this->_groups as $group => $fields) {
                $array = $this->_fieldsets[$group]['array'];
                if ($array === true) {
                        if(isset($this->_fieldsets[$group]['parent'])) {
                                $groupControl = $this->_fieldsets[$group]['parent'];
                        } else {
                                $groupControl = $group;
                        }
                } else {
                        $groupControl = $array;
                }
                // Bind if no group is specified or if the group matches the current group.
                if ($limit === null || ($limit !== null && $group === $limit)) {
                        // Iterate through the values.
                        foreach ($data as $k => $v) {
                                // If the field name matches the name of the group and the value is not scalar, recurse.
                                if ($k == $groupControl && !is_scalar($v) && !is_resource($v)) {
                                        if(isset($this->_fieldsets[$group]['children']))
                                        {
                                                $childgroups = $this->_fieldsets[$group]['children'];
                                                array_unshift($childgroups, $group);
                                                foreach($childgroups as $cgroup)
                                                {
                                                        $this->bind($v, $cgroup);
                                                }
                                        } else {
                                                $this->bind($v, $group);
                                        }
                                } else {
                                        // Bind the value to the field if it exists.
                                        if (isset($this->_groups[$group][$k]) && is_object($this->_groups[$group][$k])) {
                                                $this->_data[$group][$k] = $v;
                                        }
                                        if (isset($this->_fieldsets[$group]['parent']) && isset($this->_groups[$this->_fieldsets[$group]['parent']][$k]) && is_object($this->_groups[$this->_fieldsets[$group]['parent']][$k])) {
                                                $this->_data[$group][$k] = $v;
                                        }
                                }
                        }
                }
        }
 
        return true;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />