📄 form.php
字号:
require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception('No valid elements specified for display group'); } $name = (string) $name; if (is_array($options)) { $options['elements'] = $group; } elseif ($options instanceof Zend_Config) { $options = $options->toArray(); $options['elements'] = $group; } else { $options = array('elements' => $group); } if (isset($options['displayGroupClass'])) { $class = $options['displayGroupClass']; unset($options['displayGroupClass']); } else { $class = $this->getDefaultDisplayGroupClass(); } if (!class_exists($class)) { require_once 'Zend/Loader.php'; Zend_Loader::loadClass($class); } $this->_displayGroups[$name] = new $class( $name, $this->getPluginLoader(self::DECORATOR) ); if (!empty($this->_displayGroupPrefixPaths)) { $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths); } if (is_array($options)) { $this->_displayGroups[$name]->setOptions($options); } elseif ($options instanceof Zend_Config) { $this->_displayGroups[$name]->setConfig($options); } $this->_order[$name] = $this->_displayGroups[$name]->getOrder(); $this->_orderUpdated = true; return $this; } /** * Add multiple display groups at once * * @param array $groups * @return Zend_Form */ public function addDisplayGroups(array $groups) { foreach ($groups as $key => $spec) { $name = null; if (!is_numeric($key)) { $name = $key; } if (!is_array($spec) || empty($spec)) { continue; } $argc = count($spec); $options = array(); if (isset($spec['elements'])) { $elements = $spec['elements']; if (isset($spec['name'])) { $name = $spec['name']; } if (isset($spec['options'])) { $options = $spec['options']; } $this->addDisplayGroup($elements, $name, $options); } else { switch ($argc) { case (1 <= $argc): $elements = array_shift($spec); if (!is_array($elements) && (null !== $name)) { $elements = array_merge((array) $elements, $spec); $this->addDisplayGroup($elements, $name); break; } case (2 <= $argc): if (null !== $name) { $options = array_shift($spec); $this->addDisplayGroup($elements, $name, $options); break; } $name = array_shift($spec); case (3 <= $argc): $options = array_shift($spec); default: $this->addDisplayGroup($elements, $name, $options); } } } return $this; } /** * Add multiple display groups (overwrites) * * @param array $groups * @return Zend_Form */ public function setDisplayGroups(array $groups) { return $this->clearDisplayGroups() ->addDisplayGroups($groups); } /** * Return a display group * * @param string $name * @return array|null */ public function getDisplayGroup($name) { $name = (string) $name; if (isset($this->_displayGroups[$name])) { return $this->_displayGroups[$name]; } return null; } /** * Return all display groups * * @return array */ public function getDisplayGroups() { return $this->_displayGroups; } /** * Remove a display group by name * * @param string $name * @return boolean */ public function removeDisplayGroup($name) { $name = (string) $name; if (array_key_exists($name, $this->_displayGroups)) { foreach ($this->_displayGroups[$name] as $key => $element) { if (array_key_exists($key, $this->_elements)) { $this->_order[$key] = $element->getOrder(); $this->_orderUpdated = true; } } unset($this->_displayGroups[$name]); if (array_key_exists($name, $this->_order)) { unset($this->_order[$name]); $this->_orderUpdated = true; } return true; } return false; } /** * Remove all display groups * * @return Zend_Form */ public function clearDisplayGroups() { foreach ($this->_displayGroups as $key => $group) { if (array_key_exists($key, $this->_order)) { unset($this->_order[$key]); } foreach ($group as $name => $element) { if (isset($this->_elements[$name])) { $this->_order[$name] = $element->getOrder(); } $this->_order[$name] = $element->getOrder(); } } $this->_displayGroups = array(); $this->_orderUpdated = true; return $this; } // Processing /** * Populate form * * Proxies to {@link setDefaults()} * * @param array $values * @return Zend_Form */ public function populate(array $values) { return $this->setDefaults($values); } /** * Determine array key name from given value * * Given a value such as foo[bar][baz], returns the last element (in this case, 'baz'). * * @param string $value * @return string */ protected function _getArrayName($value) { if (empty($value) || !is_string($value)) { return $value; } if (!strstr($value, '[')) { return $value; } $endPos = strlen($value) - 1; if (']' != $value[$endPos]) { return $value; } $start = strrpos($value, '[') + 1; $name = substr($value, $start, $endPos - $start); return $name; } /** * Validate the form * * @param array $data * @return boolean */ public function isValid($data) { if (!is_array($data)) { require_once 'Zend/Form/Exception.php'; throw new Zend_Form_Exception(__CLASS__ . '::' . __METHOD__ . ' expects an array'); } $translator = $this->getTranslator(); $valid = true; if ($this->isArray()) { $key = $this->_getArrayName($this->getElementsBelongTo()); if (isset($data[$key])) { $data = $data[$key]; } } foreach ($this->getElements() as $key => $element) { $element->setTranslator($translator); if (!isset($data[$key])) { $valid = $element->isValid(null, $data) && $valid; } else { $valid = $element->isValid($data[$key], $data) && $valid; } } foreach ($this->getSubForms() as $key => $form) { $form->setTranslator($translator); if (isset($data[$key])) { $valid = $form->isValid($data[$key]) && $valid; } else { $array = $this->_getArrayName($form->getElementsBelongTo()); if (empty($array) || !isset($data[$array])) { $valid = $form->isValid($data) && $valid; } else { $valid = $form->isValid($data[$array]) && $valid; } } } $this->_errorsExist = !$valid; return $valid; } /** * Validate a partial form * * Does not check for required flags. * * @param array $data * @return boolean */ public function isValidPartial(array $data) { if ($this->isArray()) { $key = $this->_getArrayName($this->getElementsBelongTo()); if (isset($data[$key])) { $data = $data[$key]; } } $translator = $this->getTranslator(); $valid = true; $validatedSubForms = array(); foreach ($data as $key => $value) { if (null !== ($element = $this->getElement($key))) { if (null !== $translator) { $element->setTranslator($translator); } $valid = $element->isValid($value, $data) && $valid; } elseif (null !== ($subForm = $this->getSubForm($key))) { if (null !== $translator) { $subForm->setTranslator($translator); } $valid = $subForm->isValidPartial($data[$key]) && $valid; $validatedSubForms[] = $key; } } foreach ($this->getSubForms() as $key => $subForm) { if (!in_array($key, $validatedSubForms)) { if (null !== $translator) { $subForm->setTranslator($translator); } $array = $this->_getArrayName($subForm->getElementsBelongTo()); if (empty($array) || !isset($data[$array])) { $valid = $subForm->isValidPartial($data) && $valid; } else { $valid = $subForm->isValidPartial($data[$array]) && $valid; } } } $this->_errorsExist = !$valid; return $valid; } /** * Process submitted AJAX data * * Checks if provided $data is valid, via {@link isValidPartial()}. If so, * it returns JSON-encoded boolean true. If not, it returns JSON-encoded * error messages (as returned by {@link getMessages()}). * * @param array $data * @return string JSON-encoded boolean true or error messages */ public function processAjax(array $data) { require_once 'Zend/Json.php'; if ($this->isValidPartial($data)) { return Zend_Json::encode(true); } $messages = $this->getMessages(); return Zend_Json::encode($messages); } public function persistData() { } /** * Are there errors in the form? * * @return bool */ public function isErrors() { return $this->_errorsExist; } /** * Get error codes for all elements failing validation * * @param string $name * @return array */ public function getErrors($name = null) { $errors = array(); if ((null !== $name) && isset($this->_elements[$name])) { $errors = $this->getElement($name)->getErrors(); } elseif ((null !== $name) && isset($this->_subForms[$name])) { $errors = $this->getSubForm($name)->getErrors(); } else { foreach ($this->_elements as $key => $element) { $errors[$key] = $element->getErrors(); } foreach ($this->getSubForms() as $key => $subForm) { $array = $this->_getArrayName($subForm->getElementsBelongTo()); if (empty($array) || !isset($data[$array])) { $errors[$key] = $subForm->getErrors(); } else { $errors[$array] = $subForm->getErrors(); } } } return $errors; } /** * Retrieve error messages from elements failing validations * * @param string $name * @param bool $suppressArrayNotation * @return array */ public function getMessages($name = null, $suppressArrayNotation = false) { if ((null !== $name) && isset($this->_elements[$name])) { return $this->getElement($name)->getMessages(); } if ((null !== $name) && isset($this->_subForms[$name])) { return $this->getSubForm($name)->getMessages(null, true); } $arrayKeys = array(); foreach ($this->getSubForms() as $key => $subForm) { $array = $this->_getArrayName($subForm->getElementsBelongTo()); if (!empty($array)) { if ($name == $array) { return $subForm->getMessages(null, true); } $arrayKeys[$key] = $array; } } $messages = array(); foreach ($this->getElements() as $name => $element) { $eMessages = $element->getMessages(); if (!empty($eMessages)) { $messages[$name] = $eMessages; } } foreach ($this->getSubForms() as $key => $subForm) { $fMessages = $subForm->getMessages(null, true); if (!empty($fMessages)) { if (array_key_exists($key, $arrayKeys)) { $messages[$arrayKeys[$key]] = $fMessages; } else { $messages[$key] = $fMessages; } } } if (!$suppressArrayNotation && $this->isArray()) { $messages = array( $this->getElementsBelongTo() => $messages ); } return $messages; } // Rendering /** * Set view object * * @param Zend_View_Interface $view * @return Zend_Form */ public function setView(Zend_View_Interface $view = null) { $this->_view = $view; return $this; } /** * Retrieve view object * * If none registered, attempts to pull from ViewRenderer. * * @return Zend_View_Interface|null */ public function getView()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -