📄 element.php
字号:
if (!isset($this->_validators[$name])) { $validators = array_keys($this->_validators); $len = strlen($name); foreach ($validators as $validator) { if (0 === substr_compare($validator, $name, -$len, $len, true)) { return $this->_validators[$validator]; } } return false; } return $this->_validators[$name]; } /** * Retrieve all validators * * @return array */ public function getValidators() { return $this->_validators; } /** * Remove a single validator by name * * @param string $name * @return bool */ public function removeValidator($name) { $validator = $this->getValidator($name); if ($validator) { $name = get_class($validator); unset($this->_validators[$name]); return true; } return false; } /** * Clear all validators * * @return Zend_Form_Element */ public function clearValidators() { $this->_validators = array(); return $this; } /** * Validate element value * * If a translation adapter is registered, any error messages will be * translated according to the current locale, using the given error code; * if no matching translation is found, the original message will be * utilized. * * Note: The *filtered* value is validated. * * @param mixed $value * @param mixed $context * @return boolean */ public function isValid($value, $context = null) { $this->setValue($value); $value = $this->getValue(); if (empty($value) && !$this->isRequired() && $this->getAllowEmpty()) { return true; } if ($this->isRequired() && $this->autoInsertNotEmptyValidator() && !$this->getValidator('NotEmpty')) { $validators = $this->getValidators(); $notEmpty = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true); array_unshift($validators, $notEmpty); $this->setValidators($validators); } $this->_messages = array(); $this->_errors = array(); $result = true; $translator = $this->getTranslator(); $isArray = $this->isArray(); foreach ($this->getValidators() as $key => $validator) { if (method_exists($validator, 'setTranslator')) { $validator->setTranslator($translator); } if ($isArray && is_array($value)) { $messages = array(); $errors = array(); foreach ($value as $val) { if (!$validator->isValid($val, $context)) { $result = false; $messages = array_merge($messages, $validator->getMessages()); $errors = array_merge($errors, $validator->getErrors()); } } if ($result) { continue; } } if ($validator->isValid($value, $context)) { continue; } else { $result = false; $messages = $validator->getMessages(); $errors = array_keys($messages); } $result = false; $this->_messages = array_merge($this->_messages, $messages); $this->_errors = array_merge($this->_errors, $errors); if ($validator->zfBreakChainOnFailure) { break; } } return $result; } /** * Retrieve validator chain errors * * @return array */ public function getErrors() { return $this->_errors; } /** * Retrieve error messages * * @return array */ public function getMessages() { return $this->_messages; } // Filtering /** * Add a filter to the element * * @param string|Zend_Filter_Interface $filter * @return Zend_Form_Element */ public function addFilter($filter, $options = array()) { if ($filter instanceof Zend_Filter_Interface) { $name = get_class($filter); } elseif (is_string($filter)) { $name = $this->getPluginLoader(self::FILTER)->load($filter); if (empty($options)) { $filter = new $name; } else { $r = new ReflectionClass($name); if ($r->hasMethod('__construct')) { $filter = $r->newInstanceArgs((array) $options); } else { $filter = $r->newInstance(); } } } else { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception('Invalid filter provided to addFilter; must be string or Zend_Filter_Interface'); } $this->_filters[$name] = $filter; return $this; } /** * Add filters to element * * @param array $filters * @return Zend_Form_Element */ public function addFilters(array $filters) { foreach ($filters as $filterInfo) { if (is_string($filterInfo)) { $this->addFilter($filterInfo); } elseif ($filterInfo instanceof Zend_Filter_Interface) { $this->addFilter($filterInfo); } elseif (is_array($filterInfo)) { $argc = count($filterInfo); $options = array(); if (isset($filterInfo['filter'])) { $filter = $filterInfo['filter']; if (isset($filterInfo['options'])) { $options = $filterInfo['options']; } $this->addFilter($filter, $options); } else { switch (true) { case (0 == $argc): break; case (1 <= $argc): $filter = array_shift($filterInfo); case (2 <= $argc): $options = array_shift($filterInfo); default: $this->addFilter($filter, $options); break; } } } else { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception('Invalid filter passed to addFilters()'); } } return $this; } /** * Add filters to element, overwriting any already existing * * @param array $filters * @return Zend_Form_Element */ public function setFilters(array $filters) { $this->clearFilters(); return $this->addFilters($filters); } /** * Retrieve a single filter by name * * @param string $name * @return Zend_Filter_Interface */ public function getFilter($name) { if (!isset($this->_filters[$name])) { $filters = array_keys($this->_filters); $len = strlen($name); foreach ($filters as $filter) { if (0 === substr_compare($filter, $name, -$len, $len, true)) { return $this->_filters[$filter]; } } return false; } return $this->_filters[$name]; } /** * Get all filters * * @return array */ public function getFilters() { return $this->_filters; } /** * Remove a filter by name * * @param string $name * @return Zend_Form_Element */ public function removeFilter($name) { $filter = $this->getFilter($name); if ($filter) { $name = get_class($filter); unset($this->_filters[$name]); return true; } return false; } /** * Clear all filters * * @return Zend_Form_Element */ public function clearFilters() { $this->_filters = array(); return $this; } // Rendering /** * Set view object * * @param Zend_View_Interface $view * @return Zend_Form_Element */ public function setView(Zend_View_Interface $view = null) { $this->_view = $view; return $this; } /** * Retrieve view object * * Retrieves from ViewRenderer if none previously set. * * @return null|Zend_View_Interface */ public function getView() { if (null === $this->_view) { require_once 'Zend/Controller/Action/HelperBroker.php'; $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); $this->_view = $viewRenderer->view; } return $this->_view; } /** * Instantiate a decorator based on class name or class name fragment * * @param string $name * @param null|array $options * @return Zend_Form_Decorator_Interface */ protected function _getDecorator($name, $options) { $class = $this->getPluginLoader(self::DECORATOR)->load($name); if (null === $options) { $decorator = new $class; } else { $r = new ReflectionClass($class); $decorator = $r->newInstance($options); } return $decorator; } /** * Add a decorator for rendering the element * * @param string|Zend_Form_Decorator_Interface $decorator * @param array|Zend_Config $options Options with which to initialize decorator * @return Zend_Form_Element */ public function addDecorator($decorator, $options = null) { if ($decorator instanceof Zend_Form_Decorator_Interface) { $name = get_class($decorator); } elseif (is_string($decorator)) { $decorator = $this->_getDecorator($decorator, $options); $name = get_class($decorator); } elseif (is_array($decorator)) { foreach ($decorator as $name => $spec) { break; } if (is_numeric($name)) { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string'); } if (is_string($spec)) { $decorator = $this->_getDecorator($spec, $options); } elseif ($spec instanceof Zend_Form_Decorator_Interface) { $decorator = $spec; } } else { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface'); } $this->_decorators[$name] = $decorator; return $this; } /** * Add many decorators at once * * @param array $decorators * @return Zend_Form_Element */ public function addDecorators(array $decorators) { foreach ($decorators as $decoratorInfo) { if (is_string($decoratorInfo)) { $this->addDecorator($decoratorInfo); } elseif ($decoratorInfo instanceof Zend_Form_Decorator_Interface) { $this->addDecorator($decoratorInfo); } elseif (is_array($decoratorInfo)) { $argc = count($decoratorInfo); $options = array(); if (isset($decoratorInfo['decorator'])) { $decorator = $decoratorInfo['decorator']; if (isset($decoratorInfo['options'])) { $options = $decoratorInfo['options']; } $this->addDecorator($decorator, $options); } else { switch (true) { case (0 == $argc): break; case (1 <= $argc): $decorator = array_shift($decoratorInfo); case (2 <= $argc): $options = array_shift($decoratorInfo); default: $this->addDecorator($decorator, $options); break; } } } else { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()'); } } return $this; } /** * Overwrite all decorators * * @param array $decorators * @return Zend_Form_Element */ public function setDecorators(array $decorators) { $this->clearDecorators(); return $this->addDecorators($decorators); } /** * Retrieve a registered decorator * * @param string $name * @return false|Zend_Form_Decorator_Abstract */ public function getDecorator($name) { if (!isset($this->_decorators[$name])) { $decorators = array_keys($this->_decorators); $len = strlen($name); foreach ($decorators as $decorator) { if (0 === substr_compare($decorator, $name, -$len, $len, true)) { return $this->_decorators[$decorator]; } } return false; } return $this->_decorators[$name]; } /** * Retrieve all decorators * * @return array */ public function getDecorators() { return $this->_decorators; } /** * Remove a single decorator * * @param string $name * @return bool */ public function removeDecorator($name) { $decorator = $this->getDecorator($name); if ($decorator) { $name = get_class($decorator); unset($this->_decorators[$name]); return true; } return false; } /** * Clear all decorators * * @return Zend_Form_Element */ public function clearDecorators() { $this->_decorators = array(); return $this; } /** * Render form element * * @param Zend_View_Interface $view * @return string */ public function render(Zend_View_Interface $view = null) { if (null !== $view) { $this->setView($view); } $content = ''; foreach ($this->getDecorators() as $decorator) { $decorator->setElement($this); $content = $decorator->render($content); } return $content; } /** * String representation of form element * * Proxies to {@link render()}. * * @return string */ public function __toString() { try { $return = $this->render(); return $return; } catch (Exception $e) { trigger_error($e->getMessage(), E_USER_WARNING); return ''; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -