📄 element.php
字号:
$this->_required = (bool) $flag; return $this; } /** * Is the element required? * * @return bool */ public function isRequired() { return $this->_required; } /** * Set flag indicating whether a NotEmpty validator should be inserted when element is required * * @param bool $flag * @return Zend_Form_Element */ public function setAutoInsertNotEmptyValidator($flag) { $this->_autoInsertNotEmptyValidator = (bool) $flag; return $this; } /** * Get flag indicating whether a NotEmpty validator should be inserted when element is required * * @return bool */ public function autoInsertNotEmptyValidator() { return $this->_autoInsertNotEmptyValidator; } /** * Set element description * * @param string $description * @return Zend_Form_Element */ public function setDescription($description) { $this->_description = (string) $description; return $this; } /** * Retrieve element description * * @return string */ public function getDescription() { return $this->_description; } /** * Set 'allow empty' flag * * When the allow empty flag is enabled and the required flag is false, the * element will validate with empty values. * * @param bool $flag * @return Zend_Form_Element */ public function setAllowEmpty($flag) { $this->_allowEmpty = (bool) $flag; return $this; } /** * Get 'allow empty' flag * * @return bool */ public function getAllowEmpty() { return $this->_allowEmpty; } /** * Set ignore flag (used when retrieving values at form level) * * @param bool $flag * @return Zend_Form_Element */ public function setIgnore($flag) { $this->_ignore = (bool) $flag; return $this; } /** * Get ignore flag (used when retrieving values at form level) * * @return bool */ public function getIgnore() { return $this->_ignore; } /** * Set flag indicating if element represents an array * * @param bool $flag * @return Zend_Form_Element */ public function setIsArray($flag) { $this->_isArray = (bool) $flag; return $this; } /** * Is the element representing an array? * * @return bool */ public function isArray() { return $this->_isArray; } /** * Set array to which element belongs * * @param string $array * @return Zend_Form_Element */ public function setBelongsTo($array) { $array = $this->filterName($array, true); if (!empty($array)) { $this->_belongsTo = $array; } return $this; } /** * Return array name to which element belongs * * @return string */ public function getBelongsTo() { return $this->_belongsTo; } /** * Return elment type * * @return string */ public function getType() { if (null === $this->_type) { $this->_type = get_class($this); } return $this->_type; } /** * Set element attribute * * @param string $name * @param mixed $value * @return Zend_Form_Element * @throws Zend_Form_Exception for invalid $name values */ public function setAttrib($name, $value) { $name = (string) $name; if ('_' == $name[0]) { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception(sprintf('Invalid attribute "%s"; must not contain a leading underscore', $name)); } if (null === $value) { unset($this->$name); } else { $this->$name = $value; } return $this; } /** * Set multiple attributes at once * * @param array $attribs * @return Zend_Form_Element */ public function setAttribs(array $attribs) { foreach ($attribs as $key => $value) { $this->setAttrib($key, $value); } return $this; } /** * Retrieve element attribute * * @param string $name * @return string */ public function getAttrib($name) { $name = (string) $name; if (isset($this->$name)) { return $this->$name; } return null; } /** * Return all attributes * * @return array */ public function getAttribs() { $attribs = get_object_vars($this); foreach ($attribs as $key => $value) { if ('_' == substr($key, 0, 1)) { unset($attribs[$key]); } } return $attribs; } /** * Overloading: retrieve object property * * Prevents access to properties beginning with '_'. * * @param string $key * @return mixed */ public function __get($key) { if ('_' == $key[0]) { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception(sprintf('Cannot retrieve value for protected/private property "%s"', $key)); } if (!isset($this->$key)) { return null; } return $this->$key; } /** * Overloading: set object property * * @param string $key * @param mixed $value * @return voide */ public function __set($key, $value) { $this->setAttrib($key, $value); } // Loaders /** * Set plugin loader to use for validator or filter chain * * @param Zend_Loader_PluginLoader_Interface $loader * @param string $type 'decorator', 'filter', or 'validate' * @return Zend_Form_Element * @throws Zend_Form_Exception on invalid type */ public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type) { $type = strtoupper($type); switch ($type) { case self::DECORATOR: case self::FILTER: case self::VALIDATE: $this->_loaders[$type] = $loader; return $this; default: require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type)); } } /** * Retrieve plugin loader for validator or filter chain * * Instantiates with default rules if none available for that type. Use * 'decorator', 'filter', or 'validate' for $type. * * @param string $type * @return Zend_Loader_PluginLoader * @throws Zend_Loader_Exception on invalid type. */ public function getPluginLoader($type) { $type = strtoupper($type); switch ($type) { case self::FILTER: case self::VALIDATE: $prefixSegment = ucfirst(strtolower($type)); $pathSegment = $prefixSegment; case self::DECORATOR: if (!isset($prefixSegment)) { $prefixSegment = 'Form_Decorator'; $pathSegment = 'Form/Decorator'; } if (!isset($this->_loaders[$type])) { require_once 'Zend/Loader/PluginLoader.php'; $this->_loaders[$type] = new Zend_Loader_PluginLoader( array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/') ); } return $this->_loaders[$type]; default: require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); } } /** * Add prefix path for plugin loader * * If no $type specified, assumes it is a base path for both filters and * validators, and sets each according to the following rules: * - decorators: $prefix = $prefix . '_Decorator' * - filters: $prefix = $prefix . '_Filter' * - validators: $prefix = $prefix . '_Validate' * * Otherwise, the path prefix is set on the appropriate plugin loader. * * @param string $path * @return Zend_Form_Element * @throws Zend_Form_Exception for invalid type */ public function addPrefixPath($prefix, $path, $type = null) { $type = strtoupper($type); switch ($type) { case self::DECORATOR: case self::FILTER: case self::VALIDATE: $loader = $this->getPluginLoader($type); $loader->addPrefixPath($prefix, $path); return $this; case null: $prefix = rtrim($prefix, '_'); $path = rtrim($path, DIRECTORY_SEPARATOR); foreach (array(self::DECORATOR, self::FILTER, self::VALIDATE) as $type) { $cType = ucfirst(strtolower($type)); $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR; $pluginPrefix = $prefix . '_' . $cType; $loader = $this->getPluginLoader($type); $loader->addPrefixPath($pluginPrefix, $pluginPath); } return $this; default: require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); } } /** * Add many prefix paths at once * * @param array $spec * @return Zend_Form_Element */ public function addPrefixPaths(array $spec) { if (isset($spec['prefix']) && isset($spec['path'])) { return $this->addPrefixPath($spec['prefix'], $spec['path']); } foreach ($spec as $type => $paths) { if (is_numeric($type) && is_array($paths)) { $type = null; if (isset($paths['prefix']) && isset($paths['path'])) { if (isset($paths['type'])) { $type = $paths['type']; } $this->addPrefixPath($paths['prefix'], $paths['path'], $type); } } elseif (!is_numeric($type)) { if (!isset($paths['prefix']) || !isset($paths['path'])) { foreach ($paths as $prefix => $spec) { if (is_array($spec)) { foreach ($spec as $path) { if (!is_string($path)) { continue; } $this->addPrefixPath($prefix, $path, $type); } } elseif (is_string($spec)) { $this->addPrefixPath($prefix, $spec, $type); } } } else { $this->addPrefixPath($paths['prefix'], $paths['path'], $type); } } } return $this; } // Validation /** * Add validator to validation chain * * Note: will overwrite existing validators if they are of the same class. * * @param string|Zend_Validate_Interface $validator * @param bool $breakChainOnFailure * @param array $options * @return Zend_Form_Element * @throws Zend_Form_Exception if invalid validator type */ public function addValidator($validator, $breakChainOnFailure = false, $options = array()) { if ($validator instanceof Zend_Validate_Interface) { $name = get_class($validator); } elseif (is_string($validator)) { $name = $this->getPluginLoader(self::VALIDATE)->load($validator); if (empty($options)) { $validator = new $name; } else { $messages = false; if (isset($options['messages'])) { $messages = $options['messages']; unset($options['messages']); } $r = new ReflectionClass($name); if ($r->hasMethod('__construct')) { $validator = $r->newInstanceArgs((array) $options); } else { $validator = $r->newInstance(); } if ($messages) { if (is_array($messages)) { $validator->setMessages($messages); } elseif (is_string($messages)) { $validator->setMessage($messages); } } } } else { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception('Invalid validator provided to addValidator; must be string or Zend_Validate_Interface'); } if (!isset($validator->zfBreakChainOnFailure)) { $validator->zfBreakChainOnFailure = $breakChainOnFailure; } $this->_validators[$name] = $validator; return $this; } /** * Add multiple validators * * @param array $validators * @return Zend_Form_Element */ public function addValidators(array $validators) { foreach ($validators as $validatorInfo) { if (is_string($validatorInfo)) { $this->addValidator($validatorInfo); } elseif ($validatorInfo instanceof Zend_Validate_Interface) { $this->addValidator($validatorInfo); } elseif (is_array($validatorInfo)) { $argc = count($validatorInfo); $breakChainOnFailure = false; $options = array(); if (isset($validatorInfo['validator'])) { $validator = $validatorInfo['validator']; if (isset($validatorInfo['breakChainOnFailure'])) { $breakChainOnFailure = $validatorInfo['breakChainOnFailure']; } if (isset($validatorInfo['options'])) { $options = $validatorInfo['options']; } $this->addValidator($validator, $breakChainOnFailure, $options); } else { switch (true) { case (0 == $argc): break; case (1 <= $argc): $validator = array_shift($validatorInfo); case (2 <= $argc): $breakChainOnFailure = array_shift($validatorInfo); case (3 <= $argc): $options = array_shift($validatorInfo); default: $this->addValidator($validator, $breakChainOnFailure, $options); break; } } } else { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception('Invalid validator passed to addValidators()'); } } return $this; } /** * Set multiple validators, overwriting previous validators * * @param array $validators * @return Zend_Form_Element */ public function setValidators(array $validators) { $this->clearValidators(); return $this->addValidators($validators); } /** * Retrieve a single validator by name * * @param string $name * @return Zend_Validate_Interface|false False if not found, validator otherwise */ public function getValidator($name) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -