~ubuntu-branches/ubuntu/vivid/zendframework/vivid

« back to all changes in this revision

Viewing changes to library/Zend/Form.php

  • Committer: Bazaar Package Importer
  • Author(s): Frank Habermann
  • Date: 2010-04-28 20:10:00 UTC
  • mfrom: (1.3.1 upstream) (9.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100428201000-o347bj5qb5i3tpot
Tags: 1.10.4-1
new upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 * @package    Zend_Form
29
29
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
30
30
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
31
 
 * @version    $Id: Form.php 20096 2010-01-06 02:05:09Z bkarwin $
 
31
 * @version    $Id: Form.php 21924 2010-04-17 11:55:50Z alab $
32
32
 */
33
33
class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
34
34
{
842
842
    public function setName($name)
843
843
    {
844
844
        $name = $this->filterName($name);
845
 
        if (('0' !== $name) && empty($name)) {
 
845
        if ('' === (string)$name) {
846
846
            require_once 'Zend/Form/Exception.php';
847
847
            throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
848
848
        }
1226
1226
     */
1227
1227
    public function setDefaults(array $defaults)
1228
1228
    {
 
1229
        $eBelongTo = null;
 
1230
 
 
1231
        if ($this->isArray()) {
 
1232
            $eBelongTo = $this->getElementsBelongTo();
 
1233
            $defaults = $this->_dissolveArrayValue($defaults, $eBelongTo);
 
1234
        }
1229
1235
        foreach ($this->getElements() as $name => $element) {
1230
 
            if (array_key_exists($name, $defaults)) {
1231
 
                $this->setDefault($name, $defaults[$name]);
 
1236
            $check = $defaults;
 
1237
            if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
 
1238
                $check = $this->_dissolveArrayValue($defaults, $belongsTo);
 
1239
            }
 
1240
            if (array_key_exists($name, $check)) {
 
1241
                $this->setDefault($name, $check[$name]);
 
1242
                $defaults = $this->_dissolveArrayUnsetKey($defaults, $belongsTo, $name);
1232
1243
            }
1233
1244
        }
1234
1245
        foreach ($this->getSubForms() as $name => $form) {
1235
 
            if (array_key_exists($name, $defaults)) {
 
1246
            if (!$form->isArray() && array_key_exists($name, $defaults)) {
1236
1247
                $form->setDefaults($defaults[$name]);
1237
1248
            } else {
1238
1249
                $form->setDefaults($defaults);
1298
1309
    public function getValues($suppressArrayNotation = false)
1299
1310
    {
1300
1311
        $values = array();
 
1312
        $eBelongTo = null;
 
1313
        
 
1314
        if ($this->isArray()) {
 
1315
            $eBelongTo = $this->getElementsBelongTo();
 
1316
        }
 
1317
        
1301
1318
        foreach ($this->getElements() as $key => $element) {
1302
1319
            if (!$element->getIgnore()) {
1303
 
                $values[$key] = $element->getValue();
 
1320
                $merge = array();
 
1321
                if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
 
1322
                    if ('' !== (string)$belongsTo) {
 
1323
                        $key = $belongsTo . '[' . $key . ']';
 
1324
                    }
 
1325
                }
 
1326
                $merge = $this->_attachToArray($element->getValue(), $key);
 
1327
                $values = array_merge_recursive($values, $merge);
1304
1328
            }
1305
1329
        }
1306
1330
        foreach ($this->getSubForms() as $key => $subForm) {
1307
 
            $fValues = $this->_attachToArray($subForm->getValues(true), $subForm->getElementsBelongTo());
1308
 
            $values = array_merge($values, $fValues);
 
1331
            $merge = array();
 
1332
            if (!$subForm->isArray()) {
 
1333
                $merge[$key] = $subForm->getValues();
 
1334
            } else {
 
1335
                $merge = $this->_attachToArray($subForm->getValues(true),
 
1336
                                               $subForm->getElementsBelongTo());
 
1337
            }
 
1338
            $values = array_merge_recursive($values, $merge);
1309
1339
        }
1310
1340
 
1311
1341
        if (!$suppressArrayNotation && $this->isArray()) {
1323
1353
     * values to persist them.
1324
1354
     *
1325
1355
     * @param  array $data
 
1356
     * @param  bool $suppressArrayNotation
1326
1357
     * @return array
1327
1358
     */
1328
 
    public function getValidValues($data)
 
1359
    public function getValidValues($data, $suppressArrayNotation = false)
1329
1360
    {
1330
1361
        $values = array();
 
1362
        $eBelongTo = null;
 
1363
 
 
1364
        if ($this->isArray()) {
 
1365
            $eBelongTo = $this->getElementsBelongTo();
 
1366
            $data = $this->_dissolveArrayValue($data, $eBelongTo);
 
1367
        }
 
1368
        $context = $data;
1331
1369
        foreach ($this->getElements() as $key => $element) {
1332
 
            if (isset($data[$key])) {
1333
 
                if($element->isValid($data[$key], $data)) {
1334
 
                    $values[$key] = $element->getValue();
 
1370
            $check = $data;
 
1371
            if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
 
1372
                $check = $this->_dissolveArrayValue($data, $belongsTo);
 
1373
            }
 
1374
            if (isset($check[$key])) {
 
1375
                if($element->isValid($check[$key], $context)) {
 
1376
                    $merge = array();
 
1377
                    if ($belongsTo !== $eBelongTo && '' !== (string)$belongsTo) {
 
1378
                        $key = $belongsTo . '[' . $key . ']';
 
1379
                    }
 
1380
                    $merge = $this->_attachToArray($element->getValue(), $key);
 
1381
                    $values = array_merge_recursive($values, $merge);
1335
1382
                }
 
1383
                $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
1336
1384
            }
1337
1385
        }
1338
1386
        foreach ($this->getSubForms() as $key => $form) {
1339
 
            if (isset($data[$key])) {
1340
 
                $values[$key] = $form->getValidValues($data[$key]);
 
1387
            $merge = array();
 
1388
            if (isset($data[$key]) && !$form->isArray()) {
 
1389
                $tmp = $form->getValidValues($data[$key]);
 
1390
                if (!empty($tmp)) {
 
1391
                    $merge[$key] = $tmp;
 
1392
                }
 
1393
            } else {
 
1394
                $tmp = $form->getValidValues($data, true);
 
1395
                if (!empty($tmp)) {
 
1396
                    $merge = $this->_attachToArray($tmp, $form->getElementsBelongTo());
 
1397
                }
1341
1398
            }
 
1399
            $values = array_merge_recursive($values, $merge);
 
1400
        }
 
1401
        if (!$suppressArrayNotation && $this->isArray() && !empty($values)) {
 
1402
            $values = $this->_attachToArray($values, $this->getElementsBelongTo());
1342
1403
        }
1343
1404
 
1344
1405
        return $values;
1397
1458
    {
1398
1459
        $origName = $this->getElementsBelongTo();
1399
1460
        $name = $this->filterName($array, true);
1400
 
        if (empty($name)) {
 
1461
        if ('' === $name) {
1401
1462
            $name = null;
1402
1463
        }
1403
1464
        $this->_elementsBelongTo = $name;
1449
1510
    {
1450
1511
        if ((null === $this->_elementsBelongTo) && $this->isArray()) {
1451
1512
            $name = $this->getName();
1452
 
            if (!empty($name)) {
 
1513
            if ('' !== (string)$name) {
1453
1514
                return $name;
1454
1515
            }
1455
1516
        }
1742
1803
    {
1743
1804
        if (null === $name) {
1744
1805
            $name = $group->getName();
1745
 
            if (empty($name)) {
 
1806
            if ('' === (string)$name) {
1746
1807
                require_once 'Zend/Form/Exception.php';
1747
1808
                throw new Zend_Form_Exception('Invalid display group added; requires name');
1748
1809
            }
1934
1995
     */
1935
1996
    protected function _getArrayName($value)
1936
1997
    {
1937
 
        if (empty($value) || !is_string($value)) {
 
1998
        if (!is_string($value) || '' === $value) {
1938
1999
            return $value;
1939
2000
        }
1940
2001
 
1986
2047
    }
1987
2048
 
1988
2049
    /**
 
2050
     * Given an array, an optional arrayPath and a key this method
 
2051
     * dissolves the arrayPath and unsets the key within the array
 
2052
     * if it exists.
 
2053
     * 
 
2054
     * @param array $array 
 
2055
     * @param string|null $arrayPath
 
2056
     * @param string $key
 
2057
     * @return array
 
2058
     */
 
2059
    protected function _dissolveArrayUnsetKey($array, $arrayPath, $key)
 
2060
    {
 
2061
        $unset =& $array;
 
2062
        $path  = trim(strtr((string)$arrayPath, array('[' => '/', ']' => '')), '/');
 
2063
        $segs  = ('' !== $path) ? explode('/', $path) : array();
 
2064
        
 
2065
        foreach ($segs as $seg) {
 
2066
            if (!array_key_exists($seg, (array)$unset)) {
 
2067
                return $array;
 
2068
            }
 
2069
            $unset =& $unset[$seg];
 
2070
        }
 
2071
        if (array_key_exists($key, (array)$unset)) {
 
2072
            unset($unset[$key]);
 
2073
        }
 
2074
        return $array;
 
2075
    }
 
2076
 
 
2077
    /**
1989
2078
     * Converts given arrayPath to an array and attaches given value at the end of it.
1990
2079
     *
1991
2080
     * @param  mixed $value The value to attach
2025
2114
        }
2026
2115
        $translator = $this->getTranslator();
2027
2116
        $valid      = true;
 
2117
        $eBelongTo  = null;
2028
2118
 
2029
2119
        if ($this->isArray()) {
2030
 
            $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
 
2120
            $eBelongTo = $this->getElementsBelongTo();
 
2121
            $data = $this->_dissolveArrayValue($data, $eBelongTo);
2031
2122
        }
2032
 
 
 
2123
        $context = $data;
2033
2124
        foreach ($this->getElements() as $key => $element) {
2034
 
            $element->setTranslator($translator);
2035
 
            if (!isset($data[$key])) {
2036
 
                $valid = $element->isValid(null, $data) && $valid;
 
2125
            if (null !== $translator && !$element->hasTranslator()) {
 
2126
                $element->setTranslator($translator);
 
2127
            }
 
2128
            $check = $data;
 
2129
            if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
 
2130
                $check = $this->_dissolveArrayValue($data, $belongsTo);
 
2131
            }
 
2132
            if (!isset($check[$key])) {
 
2133
                $valid = $element->isValid(null, $context) && $valid;
2037
2134
            } else {
2038
 
                $valid = $element->isValid($data[$key], $data) && $valid;
 
2135
                $valid = $element->isValid($check[$key], $context) && $valid;
 
2136
                $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
2039
2137
            }
2040
2138
        }
2041
2139
        foreach ($this->getSubForms() as $key => $form) {
2042
 
            $form->setTranslator($translator);
2043
 
            if (isset($data[$key])) {
 
2140
            if (null !== $translator && !$form->hasTranslator()) {
 
2141
                $form->setTranslator($translator);
 
2142
            }
 
2143
            if (isset($data[$key]) && !$form->isArray()) {
2044
2144
                $valid = $form->isValid($data[$key]) && $valid;
2045
2145
            } else {
2046
2146
                $valid = $form->isValid($data) && $valid;
2067
2167
     */
2068
2168
    public function isValidPartial(array $data)
2069
2169
    {
 
2170
        $eBelongTo  = null;
 
2171
 
2070
2172
        if ($this->isArray()) {
2071
 
            $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
 
2173
            $eBelongTo = $this->getElementsBelongTo();
 
2174
            $data = $this->_dissolveArrayValue($data, $eBelongTo);
2072
2175
        }
2073
2176
 
2074
 
        $translator        = $this->getTranslator();
2075
 
        $valid             = true;
2076
 
        $validatedSubForms = array();
 
2177
        $translator = $this->getTranslator();
 
2178
        $valid      = true;
 
2179
        $context    = $data;
2077
2180
 
2078
 
        foreach ($data as $key => $value) {
2079
 
            if (null !== ($element = $this->getElement($key))) {
2080
 
                if (null !== $translator) {
 
2181
        foreach ($this->getElements() as $key => $element) {
 
2182
            $check = $data;
 
2183
            if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
 
2184
                $check = $this->_dissolveArrayValue($data, $belongsTo);
 
2185
            }
 
2186
            if (isset($check[$key])) {
 
2187
                if (null !== $translator && !$element->hasTranslator()) {
2081
2188
                    $element->setTranslator($translator);
2082
2189
                }
2083
 
                $valid = $element->isValid($value, $data) && $valid;
2084
 
            } elseif (null !== ($subForm = $this->getSubForm($key))) {
2085
 
                if (null !== $translator) {
2086
 
                    $subForm->setTranslator($translator);
2087
 
                }
2088
 
                $valid = $subForm->isValidPartial($data[$key]) && $valid;
2089
 
                $validatedSubForms[] = $key;
 
2190
                $valid = $element->isValid($check[$key], $context) && $valid;
 
2191
                $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
2090
2192
            }
2091
2193
        }
2092
 
        foreach ($this->getSubForms() as $key => $subForm) {
2093
 
            if (!in_array($key, $validatedSubForms)) {
2094
 
                if (null !== $translator) {
2095
 
                    $subForm->setTranslator($translator);
2096
 
                }
2097
 
 
2098
 
                $valid = $subForm->isValidPartial($data) && $valid;
 
2194
        foreach ($this->getSubForms() as $key => $form) {
 
2195
            if (null !== $translator && !$form->hasTranslator()) {
 
2196
                $form->setTranslator($translator);
 
2197
            }
 
2198
            if (isset($data[$key]) && !$form->isArray()) {
 
2199
                $valid = $form->isValidPartial($data[$key]) && $valid;
 
2200
            } else {
 
2201
                $valid = $form->isValidPartial($data) && $valid;
2099
2202
            }
2100
2203
        }
2101
2204
 
2254
2357
     * @param  string $name
2255
2358
     * @return array
2256
2359
     */
2257
 
    public function getErrors($name = null)
 
2360
    public function getErrors($name = null, $suppressArrayNotation = false)
2258
2361
    {
2259
2362
        $errors = array();
2260
 
        if ((null !== $name) && isset($this->_elements[$name])) {
2261
 
            $errors = $this->getElement($name)->getErrors();
2262
 
        } elseif ((null !== $name) && isset($this->_subForms[$name])) {
2263
 
            $errors = $this->getSubForm($name)->getErrors();
2264
 
        } else {
2265
 
            foreach ($this->_elements as $key => $element) {
2266
 
                $errors[$key] = $element->getErrors();
2267
 
            }
2268
 
            foreach ($this->getSubForms() as $key => $subForm) {
2269
 
                $fErrors = $this->_attachToArray($subForm->getErrors(), $subForm->getElementsBelongTo());
2270
 
                $errors = array_merge($errors, $fErrors);
2271
 
            }
2272
 
        }
 
2363
        if (null !== $name) {
 
2364
            if (isset($this->_elements[$name])) {
 
2365
                return $this->getElement($name)->getErrors();
 
2366
            } else if (isset($this->_subForms[$name])) {
 
2367
                return $this->getSubForm($name)->getErrors(null, true);
 
2368
            }
 
2369
        }
 
2370
        
 
2371
        foreach ($this->_elements as $key => $element) {
 
2372
            $errors[$key] = $element->getErrors();
 
2373
        }
 
2374
        foreach ($this->getSubForms() as $key => $subForm) {
 
2375
            $merge = array();
 
2376
            if (!$subForm->isArray()) {
 
2377
                $merge[$key] = $subForm->getErrors();
 
2378
            } else {
 
2379
                $merge = $this->_attachToArray($subForm->getErrors(null, true),
 
2380
                                               $subForm->getElementsBelongTo());
 
2381
            }
 
2382
            $errors = array_merge_recursive($errors, $merge);
 
2383
        }
 
2384
 
 
2385
        if (!$suppressArrayNotation && $this->isArray()) {
 
2386
            $errors = $this->_attachToArray($errors, $this->getElementsBelongTo());
 
2387
        }
 
2388
 
2273
2389
        return $errors;
2274
2390
    }
2275
2391
 
2282
2398
     */
2283
2399
    public function getMessages($name = null, $suppressArrayNotation = false)
2284
2400
    {
2285
 
        if ((null !== $name) && isset($this->_elements[$name])) {
2286
 
            return $this->getElement($name)->getMessages();
2287
 
        }
2288
 
 
2289
 
        if ((null !== $name) && isset($this->_subForms[$name])) {
2290
 
            return $this->getSubForm($name)->getMessages(null, true);
2291
 
        }
2292
 
 
2293
 
        $arrayKeys = array();
2294
 
        foreach ($this->getSubForms() as $key => $subForm) {
2295
 
            $array = $this->_getArrayName($subForm->getElementsBelongTo());
2296
 
            if (!empty($array)) {
2297
 
                if ($name == $array) {
2298
 
                    return $subForm->getMessages(null, true);
 
2401
        if (null !== $name) {
 
2402
            if (isset($this->_elements[$name])) {
 
2403
                return $this->getElement($name)->getMessages();
 
2404
            } else if (isset($this->_subForms[$name])) {
 
2405
                return $this->getSubForm($name)->getMessages(null, true);
 
2406
            }
 
2407
            foreach ($this->getSubForms() as $key => $subForm) {
 
2408
                if ($subForm->isArray()) {
 
2409
                    $belongTo = $subForm->getElementsBelongTo();
 
2410
                    if ($name == $this->_getArrayName($belongTo)) {
 
2411
                        return $subForm->getMessages(null, true);
 
2412
                    }
2299
2413
                }
2300
 
                $arrayKeys[$key] = $subForm->getElementsBelongTo();
2301
2414
            }
2302
2415
        }
2303
2416
 
2316
2429
        }
2317
2430
 
2318
2431
        foreach ($this->getSubForms() as $key => $subForm) {
2319
 
            $fMessages = $subForm->getMessages(null, true);
2320
 
            if (!empty($fMessages)) {
2321
 
                if (array_key_exists($key, $arrayKeys)) {
2322
 
                    $fMessages = $this->_attachToArray($fMessages, $arrayKeys[$key]);
2323
 
                    $messages = array_merge($messages, $fMessages);
 
2432
            $merge = $subForm->getMessages(null, true);
 
2433
            if (!empty($merge)) {
 
2434
                if (!$subForm->isArray()) {
 
2435
                    $merge = array($key => $merge);
2324
2436
                } else {
2325
 
                    $messages[$key] = $fMessages;
 
2437
                    $merge = $this->_attachToArray($merge,
 
2438
                                                   $subForm->getElementsBelongTo());
2326
2439
                }
 
2440
                $messages = array_merge_recursive($messages, $merge);
2327
2441
            }
2328
2442
        }
2329
2443
 
2739
2853
 
2740
2854
        return $this->_translator;
2741
2855
    }
 
2856
    
 
2857
    /**
 
2858
     * Does this form have its own specific translator?
 
2859
     * 
 
2860
     * @return bool
 
2861
     */
 
2862
    public function hasTranslator()
 
2863
    {
 
2864
        return (bool)$this->_translator;
 
2865
    }    
2742
2866
 
2743
2867
    /**
2744
2868
     * Get global default translator object
2762
2886
    }
2763
2887
 
2764
2888
    /**
 
2889
     * Is there a default translation object set?
 
2890
     * 
 
2891
     * @return boolean
 
2892
     */
 
2893
    public static function hasDefaultTranslator()
 
2894
    { 
 
2895
        return (bool)self::$_translatorDefault;
 
2896
    }
 
2897
    
 
2898
    /**
2765
2899
     * Indicate whether or not translation should be disabled
2766
2900
     *
2767
2901
     * @param  bool $flag