web/lib/Zend/Form/Element.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    12  * obtain it through the world-wide-web, please send an email
    12  * obtain it through the world-wide-web, please send an email
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Form
    16  * @package    Zend_Form
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  */
    19  */
    20 
    20 
    21 /** @see Zend_Filter */
    21 /** @see Zend_Filter */
    22 require_once 'Zend/Filter.php';
    22 require_once 'Zend/Filter.php';
    34  * Zend_Form_Element
    34  * Zend_Form_Element
    35  *
    35  *
    36  * @category   Zend
    36  * @category   Zend
    37  * @package    Zend_Form
    37  * @package    Zend_Form
    38  * @subpackage Element
    38  * @subpackage Element
    39  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    39  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    40  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    40  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    41  * @version    $Id: Element.php 22464 2010-06-19 17:31:21Z alab $
    41  * @version    $Id: Element.php 25173 2012-12-22 20:05:32Z rob $
    42  */
    42  */
    43 class Zend_Form_Element implements Zend_Validate_Interface
    43 class Zend_Form_Element implements Zend_Validate_Interface
    44 {
    44 {
    45     /**
    45     /**
    46      * Element Constants
    46      * Element Constants
   313             return $this;
   313             return $this;
   314         }
   314         }
   315 
   315 
   316         $decorators = $this->getDecorators();
   316         $decorators = $this->getDecorators();
   317         if (empty($decorators)) {
   317         if (empty($decorators)) {
   318             $getId = create_function('$decorator',
       
   319                                      'return $decorator->getElement()->getId()
       
   320                                              . "-element";');
       
   321             $this->addDecorator('ViewHelper')
   318             $this->addDecorator('ViewHelper')
   322                  ->addDecorator('Errors')
   319                  ->addDecorator('Errors')
   323                  ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
   320                  ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
   324                  ->addDecorator('HtmlTag', array('tag' => 'dd',
   321                  ->addDecorator('HtmlTag', array(
   325                                                  'id'  => array('callback' => $getId)))
   322                      'tag' => 'dd',
       
   323                      'id'  => array('callback' => array(get_class($this), 'resolveElementId'))
       
   324                  ))
   326                  ->addDecorator('Label', array('tag' => 'dt'));
   325                  ->addDecorator('Label', array('tag' => 'dt'));
   327         }
   326         }
   328         return $this;
   327         return $this;
       
   328     }
       
   329 
       
   330     /**
       
   331      * Used to resolve and return an element ID
       
   332      *
       
   333      * Passed to the HtmlTag decorator as a callback in order to provide an ID.
       
   334      * 
       
   335      * @param  Zend_Form_Decorator_Interface $decorator 
       
   336      * @return string
       
   337      */
       
   338     public static function resolveElementId(Zend_Form_Decorator_Interface $decorator)
       
   339     {
       
   340         return $decorator->getElement()->getId() . '-element';
   329     }
   341     }
   330 
   342 
   331     /**
   343     /**
   332      * Set object state from options array
   344      * Set object state from options array
   333      *
   345      *
   890      * @return array
   902      * @return array
   891      */
   903      */
   892     public function getAttribs()
   904     public function getAttribs()
   893     {
   905     {
   894         $attribs = get_object_vars($this);
   906         $attribs = get_object_vars($this);
       
   907         unset($attribs['helper']);
   895         foreach ($attribs as $key => $value) {
   908         foreach ($attribs as $key => $value) {
   896             if ('_' == substr($key, 0, 1)) {
   909             if ('_' == substr($key, 0, 1)) {
   897                 unset($attribs[$key]);
   910                 unset($attribs[$key]);
   898             }
   911             }
   899         }
   912         }
  1056             case self::VALIDATE:
  1069             case self::VALIDATE:
  1057                 $loader = $this->getPluginLoader($type);
  1070                 $loader = $this->getPluginLoader($type);
  1058                 $loader->addPrefixPath($prefix, $path);
  1071                 $loader->addPrefixPath($prefix, $path);
  1059                 return $this;
  1072                 return $this;
  1060             case null:
  1073             case null:
  1061                 $prefix = rtrim($prefix, '_');
  1074                 $nsSeparator = (false !== strpos($prefix, '\\'))?'\\':'_';
  1062                 $path   = rtrim($path, DIRECTORY_SEPARATOR);
  1075                 $prefix = rtrim($prefix, $nsSeparator) . $nsSeparator;
       
  1076                 $path   = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  1063                 foreach (array(self::DECORATOR, self::FILTER, self::VALIDATE) as $type) {
  1077                 foreach (array(self::DECORATOR, self::FILTER, self::VALIDATE) as $type) {
  1064                     $cType        = ucfirst(strtolower($type));
  1078                     $cType        = ucfirst(strtolower($type));
  1065                     $pluginPath   = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
       
  1066                     $pluginPrefix = $prefix . '_' . $cType;
       
  1067                     $loader       = $this->getPluginLoader($type);
  1079                     $loader       = $this->getPluginLoader($type);
  1068                     $loader->addPrefixPath($pluginPrefix, $pluginPath);
  1080                     $loader->addPrefixPath($prefix . $cType, $path . $cType . DIRECTORY_SEPARATOR);
  1069                 }
  1081                 }
  1070                 return $this;
  1082                 return $this;
  1071             default:
  1083             default:
  1072                 require_once 'Zend/Form/Exception.php';
  1084                 require_once 'Zend/Form/Exception.php';
  1073                 throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  1085                 throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  1375             }
  1387             }
  1376 
  1388 
  1377             if ($isArray && is_array($value)) {
  1389             if ($isArray && is_array($value)) {
  1378                 $messages = array();
  1390                 $messages = array();
  1379                 $errors   = array();
  1391                 $errors   = array();
  1380                 foreach ($value as $val) {
  1392                 if (empty($value)) {
       
  1393                     if ($this->isRequired()
       
  1394                         || (!$this->isRequired() && !$this->getAllowEmpty())
       
  1395                     ) {
       
  1396                         $value = '';
       
  1397                     }
       
  1398                 }
       
  1399                 foreach ((array)$value as $val) {
  1381                     if (!$validator->isValid($val, $context)) {
  1400                     if (!$validator->isValid($val, $context)) {
  1382                         $result = false;
  1401                         $result = false;
  1383                         if ($this->_hasErrorMessages()) {
  1402                         if ($this->_hasErrorMessages()) {
  1384                             $messages = $this->_getErrorMessages();
  1403                             $messages = $this->_getErrorMessages();
  1385                             $errors   = $messages;
  1404                             $errors   = $messages;
  2221         $value      = $this->getValue();
  2240         $value      = $this->getValue();
  2222         foreach ($messages as $key => $message) {
  2241         foreach ($messages as $key => $message) {
  2223             if (null !== $translator) {
  2242             if (null !== $translator) {
  2224                 $message = $translator->translate($message);
  2243                 $message = $translator->translate($message);
  2225             }
  2244             }
  2226             if (($this->isArray() || is_array($value))
  2245             if ($this->isArray() || is_array($value)) {
  2227                 && !empty($value)
       
  2228             ) {
       
  2229                 $aggregateMessages = array();
  2246                 $aggregateMessages = array();
  2230                 foreach ($value as $val) {
  2247                 foreach ($value as $val) {
  2231                     $aggregateMessages[] = str_replace('%value%', $val, $message);
  2248                     $aggregateMessages[] = str_replace('%value%', $val, $message);
  2232                 }
  2249                 }
  2233                 $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages);
  2250                 if (count($aggregateMessages)) {
       
  2251                     $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages);
       
  2252                 }
  2234             } else {
  2253             } else {
  2235                 $messages[$key] = str_replace('%value%', $value, $message);
  2254                 $messages[$key] = str_replace('%value%', $value, $message);
  2236             }
  2255             }
  2237         }
  2256         }
  2238         return $messages;
  2257         return $messages;