API16

JForm/bind

From Joomla! Documentation

< API16:JForm

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.

Description[edit]

Method to recursively bind values to form fields.


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

Syntax[edit]

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[edit]

boolean True on success, false otherwise.

Defined in[edit]

libraries/joomla/form/form.php

Importing[edit]

jimport( 'joomla.form.form' );

Source Body[edit]

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 transcluded page call, red link never existed >

Examples[edit]

Code Examples[edit]