7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
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.
16 * @package Zend_Filter
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
25 require_once 'Zend/Loader.php';
30 require_once 'Zend/Filter.php';
35 require_once 'Zend/Validate.php';
39 * @package Zend_Filter
40 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
41 * @license http://framework.zend.com/license/new-bsd New BSD License
43 class Zend_Filter_Input
46 const ALLOW_EMPTY
= 'allowEmpty';
47 const BREAK_CHAIN
= 'breakChainOnFailure';
48 const DEFAULT_VALUE
= 'default';
49 const MESSAGES
= 'messages';
50 const ESCAPE_FILTER
= 'escapeFilter';
51 const FIELDS
= 'fields';
52 const FILTER
= 'filter';
53 const FILTER_CHAIN
= 'filterChain';
54 const MISSING_MESSAGE
= 'missingMessage';
55 const INPUT_NAMESPACE
= 'inputNamespace';
56 const VALIDATOR_NAMESPACE
= 'validatorNamespace';
57 const FILTER_NAMESPACE
= 'filterNamespace';
58 const NOT_EMPTY_MESSAGE
= 'notEmptyMessage';
59 const PRESENCE
= 'presence';
60 const PRESENCE_OPTIONAL
= 'optional';
61 const PRESENCE_REQUIRED
= 'required';
63 const RULE_WILDCARD
= '*';
64 const VALIDATE
= 'validate';
65 const VALIDATOR
= 'validator';
66 const VALIDATOR_CHAIN
= 'validatorChain';
67 const VALIDATOR_CHAIN_COUNT
= 'validatorChainCount';
70 * @var array Input data, before processing.
72 protected $_data = array();
75 * @var array Association of rules to filters.
77 protected $_filterRules = array();
80 * @var array Association of rules to validators.
82 protected $_validatorRules = array();
85 * @var array After processing data, this contains mapping of valid fields
88 protected $_validFields = array();
91 * @var array After processing data, this contains mapping of validation
92 * rules that did not pass validation to the array of messages returned
93 * by the validator chain.
95 protected $_invalidMessages = array();
98 * @var array After processing data, this contains mapping of validation
99 * rules that did not pass validation to the array of error identifiers
100 * returned by the validator chain.
102 protected $_invalidErrors = array();
105 * @var array After processing data, this contains mapping of validation
106 * rules in which some fields were missing to the array of messages
107 * indicating which fields were missing.
109 protected $_missingFields = array();
112 * @var array After processing, this contains a copy of $_data elements
113 * that were not mentioned in any validation rule.
115 protected $_unknownFields = array();
118 * @var Zend_Filter_Interface The filter object that is run on values
119 * returned by the getEscaped() method.
121 protected $_defaultEscapeFilter = null;
127 protected $_loaders = array();
130 * @var array Default values to use when processing filters and validators.
132 protected $_defaults = array(
133 self
::ALLOW_EMPTY
=> false,
134 self
::BREAK_CHAIN
=> false,
135 self
::ESCAPE_FILTER
=> 'HtmlEntities',
136 self
::MISSING_MESSAGE
=> "Field '%field%' is required by rule '%rule%', but the field is missing",
137 self
::NOT_EMPTY_MESSAGE
=> "You must give a non-empty value for field '%field%'",
138 self
::PRESENCE
=> self
::PRESENCE_OPTIONAL
142 * @var boolean Set to False initially, this is set to True after the
143 * input data have been processed. Reset to False in setData() method.
145 protected $_processed = false;
149 * @var Zend_Translate
151 protected $_translator;
154 * Is translation disabled?
157 protected $_translatorDisabled = false;
160 * @param array $filterRules
161 * @param array $validatorRules
162 * @param array $data OPTIONAL
163 * @param array $options OPTIONAL
165 public function __construct($filterRules, $validatorRules, array $data = null, array $options = null)
168 $this->setOptions($options);
171 $this->_filterRules
= (array) $filterRules;
172 $this->_validatorRules
= (array) $validatorRules;
175 $this->setData($data);
180 * @param mixed $namespaces
181 * @return Zend_Filter_Input
182 * @deprecated since 1.5.0RC1 - use addFilterPrefixPath() or addValidatorPrefixPath instead.
184 public function addNamespace($namespaces)
186 if (!is_array($namespaces)) {
187 $namespaces = array($namespaces);
190 foreach ($namespaces as $namespace) {
191 $prefix = $namespace;
192 $path = str_replace('_', DIRECTORY_SEPARATOR
, $prefix);
193 $this->addFilterPrefixPath($prefix, $path);
194 $this->addValidatorPrefixPath($prefix, $path);
201 * Add prefix path for all elements
203 * @param string $prefix
204 * @param string $path
205 * @return Zend_Filter_Input
207 public function addFilterPrefixPath($prefix, $path)
209 $this->getPluginLoader(self
::FILTER
)->addPrefixPath($prefix, $path);
215 * Add prefix path for all elements
217 * @param string $prefix
218 * @param string $path
219 * @return Zend_Filter_Input
221 public function addValidatorPrefixPath($prefix, $path)
223 $this->getPluginLoader(self
::VALIDATE
)->addPrefixPath($prefix, $path);
229 * Set plugin loaders for use with decorators and elements
231 * @param Zend_Loader_PluginLoader_Interface $loader
232 * @param string $type 'filter' or 'validate'
233 * @return Zend_Filter_Input
234 * @throws Zend_Filter_Exception on invalid type
236 public function setPluginLoader(Zend_Loader_PluginLoader_Interface
$loader, $type)
238 $type = strtolower($type);
242 $this->_loaders
[$type] = $loader;
245 require_once 'Zend/Filter/Exception.php';
246 throw new Zend_Filter_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
253 * Retrieve plugin loader for given type
255 * $type may be one of:
259 * If a plugin loader does not exist for the given type, defaults are
262 * @param string $type 'filter' or 'validate'
263 * @return Zend_Loader_PluginLoader_Interface
264 * @throws Zend_Filter_Exception on invalid type
266 public function getPluginLoader($type)
268 $type = strtolower($type);
269 if (!isset($this->_loaders
[$type])) {
272 $prefixSegment = 'Zend_Filter_';
273 $pathSegment = 'Zend/Filter/';
276 $prefixSegment = 'Zend_Validate_';
277 $pathSegment = 'Zend/Validate/';
280 require_once 'Zend/Filter/Exception.php';
281 throw new Zend_Filter_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
284 require_once 'Zend/Loader/PluginLoader.php';
285 $this->_loaders
[$type] = new Zend_Loader_PluginLoader(
286 array($prefixSegment => $pathSegment)
290 return $this->_loaders
[$type];
296 public function getMessages()
299 return array_merge($this->_invalidMessages
, $this->_missingFields
);
305 public function getErrors()
308 return $this->_invalidErrors
;
314 public function getInvalid()
317 return $this->_invalidMessages
;
323 public function getMissing()
326 return $this->_missingFields
;
332 public function getUnknown()
335 return $this->_unknownFields
;
339 * @param string $fieldName OPTIONAL
342 public function getEscaped($fieldName = null)
345 $this->_getDefaultEscapeFilter();
347 if ($fieldName === null) {
348 return $this->_escapeRecursive($this->_validFields
);
350 if (array_key_exists($fieldName, $this->_validFields
)) {
351 return $this->_escapeRecursive($this->_validFields
[$fieldName]);
357 * @param mixed $value
360 protected function _escapeRecursive($data)
366 if (!is_array($data)) {
367 return $this->_getDefaultEscapeFilter()->filter($data);
369 foreach ($data as &$element) {
370 $element = $this->_escapeRecursive($element);
376 * @param string $fieldName OPTIONAL
379 public function getUnescaped($fieldName = null)
382 if ($fieldName === null) {
383 return $this->_validFields
;
385 if (array_key_exists($fieldName, $this->_validFields
)) {
386 return $this->_validFields
[$fieldName];
392 * @param string $fieldName
395 public function __get($fieldName)
397 return $this->getEscaped($fieldName);
403 public function hasInvalid()
406 return !(empty($this->_invalidMessages
));
412 public function hasMissing()
415 return !(empty($this->_missingFields
));
421 public function hasUnknown()
424 return !(empty($this->_unknownFields
));
430 public function hasValid()
433 return !(empty($this->_validFields
));
437 * @param string $fieldName
440 public function isValid($fieldName = null)
443 if ($fieldName === null) {
444 return !($this->hasMissing() ||
$this->hasInvalid());
446 return array_key_exists($fieldName, $this->_validFields
);
450 * @param string $fieldName
453 public function __isset($fieldName)
456 return isset($this->_validFields
[$fieldName]);
460 * @return Zend_Filter_Input
461 * @throws Zend_Filter_Exception
463 public function process()
466 if ($this->hasInvalid()) {
467 require_once 'Zend/Filter/Exception.php';
468 throw new Zend_Filter_Exception("Input has invalid fields");
470 if ($this->hasMissing()) {
471 require_once 'Zend/Filter/Exception.php';
472 throw new Zend_Filter_Exception("Input has missing fields");
480 * @return Zend_Filter_Input
482 public function setData(array $data)
484 $this->_data
= $data;
487 * Reset to initial state
489 $this->_validFields
= array();
490 $this->_invalidMessages
= array();
491 $this->_invalidErrors
= array();
492 $this->_missingFields
= array();
493 $this->_unknownFields
= array();
495 $this->_processed
= false;
501 * @param mixed $escapeFilter
502 * @return Zend_Filter_Interface
504 public function setDefaultEscapeFilter($escapeFilter)
506 if (is_string($escapeFilter) ||
is_array($escapeFilter)) {
507 $escapeFilter = $this->_getFilter($escapeFilter);
509 if (!$escapeFilter instanceof Zend_Filter_Interface
) {
510 require_once 'Zend/Filter/Exception.php';
511 throw new Zend_Filter_Exception('Escape filter specified does not implement Zend_Filter_Interface');
513 $this->_defaultEscapeFilter
= $escapeFilter;
514 return $escapeFilter;
518 * @param array $options
519 * @return Zend_Filter_Input
520 * @throws Zend_Filter_Exception if an unknown option is given
522 public function setOptions(array $options)
524 foreach ($options as $option => $value) {
526 case self
::ESCAPE_FILTER
:
527 $this->setDefaultEscapeFilter($value);
529 case self
::INPUT_NAMESPACE
:
530 $this->addNamespace($value);
532 case self
::VALIDATOR_NAMESPACE
:
533 if(is_string($value)) {
534 $value = array($value);
537 foreach($value AS $prefix) {
538 $this->addValidatorPrefixPath(
540 str_replace('_', DIRECTORY_SEPARATOR
, $prefix)
544 case self
::FILTER_NAMESPACE
:
545 if(is_string($value)) {
546 $value = array($value);
549 foreach($value AS $prefix) {
550 $this->addFilterPrefixPath(
552 str_replace('_', DIRECTORY_SEPARATOR
, $prefix)
556 case self
::ALLOW_EMPTY
:
557 case self
::BREAK_CHAIN
:
558 case self
::MISSING_MESSAGE
:
559 case self
::NOT_EMPTY_MESSAGE
:
561 $this->_defaults
[$option] = $value;
564 require_once 'Zend/Filter/Exception.php';
565 throw new Zend_Filter_Exception("Unknown option '$option'");
574 * Set translation object
576 * @param Zend_Translate|Zend_Translate_Adapter|null $translator
577 * @return Zend_Filter_Input
579 public function setTranslator($translator = null)
581 if ((null === $translator) ||
($translator instanceof Zend_Translate_Adapter
)) {
582 $this->_translator
= $translator;
583 } elseif ($translator instanceof Zend_Translate
) {
584 $this->_translator
= $translator->getAdapter();
586 require_once 'Zend/Validate/Exception.php';
587 throw new Zend_Validate_Exception('Invalid translator specified');
594 * Return translation object
596 * @return Zend_Translate_Adapter|null
598 public function getTranslator()
600 if ($this->translatorIsDisabled()) {
604 if ($this->_translator
=== null) {
605 require_once 'Zend/Registry.php';
606 if (Zend_Registry
::isRegistered('Zend_Translate')) {
607 $translator = Zend_Registry
::get('Zend_Translate');
608 if ($translator instanceof Zend_Translate_Adapter
) {
610 } elseif ($translator instanceof Zend_Translate
) {
611 return $translator->getAdapter();
616 return $this->_translator
;
620 * Indicate whether or not translation should be disabled
623 * @return Zend_Filter_Input
625 public function setDisableTranslator($flag)
627 $this->_translatorDisabled
= (bool) $flag;
632 * Is translation disabled?
636 public function translatorIsDisabled()
638 return $this->_translatorDisabled
;
648 protected function _filter()
650 foreach ($this->_filterRules
as $ruleName => &$filterRule) {
652 * Make sure we have an array representing this filter chain.
653 * Don't typecast to (array) because it might be a Zend_Filter object
655 if (!is_array($filterRule)) {
656 $filterRule = array($filterRule);
660 * Filters are indexed by integer, metacommands are indexed by string.
661 * Pick out the filters.
663 $filterList = array();
664 foreach ($filterRule as $key => $value) {
666 $filterList[] = $value;
671 * Use defaults for filter metacommands.
673 $filterRule[self
::RULE
] = $ruleName;
674 if (!isset($filterRule[self
::FIELDS
])) {
675 $filterRule[self
::FIELDS
] = $ruleName;
679 * Load all the filter classes and add them to the chain.
681 if (!isset($filterRule[self
::FILTER_CHAIN
])) {
682 $filterRule[self
::FILTER_CHAIN
] = new Zend_Filter();
683 foreach ($filterList as $filter) {
684 if (is_string($filter) ||
is_array($filter)) {
685 $filter = $this->_getFilter($filter);
687 $filterRule[self
::FILTER_CHAIN
]->addFilter($filter);
692 * If the ruleName is the special wildcard rule,
693 * then apply the filter chain to all input data.
694 * Else just process the field named by the rule.
696 if ($ruleName == self
::RULE_WILDCARD
) {
697 foreach (array_keys($this->_data
) as $field) {
698 $this->_filterRule(array_merge($filterRule, array(self
::FIELDS
=> $field)));
701 $this->_filterRule($filterRule);
707 * @param array $filterRule
710 protected function _filterRule(array $filterRule)
712 $field = $filterRule[self
::FIELDS
];
713 if (!array_key_exists($field, $this->_data
)) {
716 if (is_array($this->_data
[$field])) {
717 foreach ($this->_data
[$field] as $key => $value) {
718 $this->_data
[$field][$key] = $filterRule[self
::FILTER_CHAIN
]->filter($value);
721 $this->_data
[$field] =
722 $filterRule[self
::FILTER_CHAIN
]->filter($this->_data
[$field]);
727 * @return Zend_Filter_Interface
729 protected function _getDefaultEscapeFilter()
731 if ($this->_defaultEscapeFilter
!== null) {
732 return $this->_defaultEscapeFilter
;
734 return $this->setDefaultEscapeFilter($this->_defaults
[self
::ESCAPE_FILTER
]);
738 * @param string $rule
739 * @param string $field
742 protected function _getMissingMessage($rule, $field)
744 $message = $this->_defaults
[self
::MISSING_MESSAGE
];
746 if (null !== ($translator = $this->getTranslator())) {
747 if ($translator->isTranslated($message)) {
748 $message = $translator->translate($message);
750 $message = $translator->translate(self
::MISSING_MESSAGE
);
754 $message = str_replace('%rule%', $rule, $message);
755 $message = str_replace('%field%', $field, $message);
762 protected function _getNotEmptyMessage($rule, $field)
764 $message = $this->_defaults
[self
::NOT_EMPTY_MESSAGE
];
766 if (null !== ($translator = $this->getTranslator())) {
767 if ($translator->isTranslated($message)) {
768 $message = $translator->translate($message);
770 $message = $translator->translate(self
::NOT_EMPTY_MESSAGE
);
774 $message = str_replace('%rule%', $rule, $message);
775 $message = str_replace('%field%', $field, $message);
782 protected function _process()
784 if ($this->_processed
=== false) {
787 $this->_processed
= true;
794 protected function _validate()
797 * Special case: if there are no validators, treat all fields as valid.
799 if (!$this->_validatorRules
) {
800 $this->_validFields
= $this->_data
;
801 $this->_data
= array();
805 foreach ($this->_validatorRules
as $ruleName => &$validatorRule) {
807 * Make sure we have an array representing this validator chain.
808 * Don't typecast to (array) because it might be a Zend_Validate object
810 if (!is_array($validatorRule)) {
811 $validatorRule = array($validatorRule);
815 * Validators are indexed by integer, metacommands are indexed by string.
816 * Pick out the validators.
818 $validatorList = array();
819 foreach ($validatorRule as $key => $value) {
821 $validatorList[$key] = $value;
826 * Use defaults for validation metacommands.
828 $validatorRule[self
::RULE
] = $ruleName;
829 if (!isset($validatorRule[self
::FIELDS
])) {
830 $validatorRule[self
::FIELDS
] = $ruleName;
832 if (!isset($validatorRule[self
::BREAK_CHAIN
])) {
833 $validatorRule[self
::BREAK_CHAIN
] = $this->_defaults
[self
::BREAK_CHAIN
];
835 if (!isset($validatorRule[self
::PRESENCE
])) {
836 $validatorRule[self
::PRESENCE
] = $this->_defaults
[self
::PRESENCE
];
838 if (!isset($validatorRule[self
::ALLOW_EMPTY
])) {
839 $validatorRule[self
::ALLOW_EMPTY
] = $this->_defaults
[self
::ALLOW_EMPTY
];
842 if (!isset($validatorRule[self
::MESSAGES
])) {
843 $validatorRule[self
::MESSAGES
] = array();
844 } else if (!is_array($validatorRule[self
::MESSAGES
])) {
845 $validatorRule[self
::MESSAGES
] = array($validatorRule[self
::MESSAGES
]);
846 } else if (array_intersect_key($validatorList, $validatorRule[self
::MESSAGES
])) {
847 // There are now corresponding numeric keys in the validation rule messages array
848 // Treat it as a named messages list for all rule validators
849 $unifiedMessages = $validatorRule[self
::MESSAGES
];
850 $validatorRule[self
::MESSAGES
] = array();
852 foreach ($validatorList as $key => $validator) {
853 if (array_key_exists($key, $unifiedMessages)) {
854 $validatorRule[self
::MESSAGES
][$key] = $unifiedMessages[$key];
860 * Load all the validator classes and add them to the chain.
862 if (!isset($validatorRule[self
::VALIDATOR_CHAIN
])) {
863 $validatorRule[self
::VALIDATOR_CHAIN
] = new Zend_Validate();
865 foreach ($validatorList as $key => $validator) {
866 if (is_string($validator) ||
is_array($validator)) {
867 $validator = $this->_getValidator($validator);
870 if (isset($validatorRule[self
::MESSAGES
][$key])) {
871 $value = $validatorRule[self
::MESSAGES
][$key];
872 if (is_array($value)) {
873 $validator->setMessages($value);
875 $validator->setMessage($value);
878 if ($validator instanceof Zend_Validate_NotEmpty
) {
879 $this->_defaults
[self
::NOT_EMPTY_MESSAGE
] = $value;
883 $validatorRule[self
::VALIDATOR_CHAIN
]->addValidator($validator, $validatorRule[self
::BREAK_CHAIN
]);
885 $validatorRule[self
::VALIDATOR_CHAIN_COUNT
] = count($validatorList);
889 * If the ruleName is the special wildcard rule,
890 * then apply the validator chain to all input data.
891 * Else just process the field named by the rule.
893 if ($ruleName == self
::RULE_WILDCARD
) {
894 foreach (array_keys($this->_data
) as $field) {
895 $this->_validateRule(array_merge($validatorRule, array(self
::FIELDS
=> $field)));
898 $this->_validateRule($validatorRule);
903 * Unset fields in $_data that have been added to other arrays.
904 * We have to wait until all rules have been processed because
905 * a given field may be referenced by multiple rules.
907 foreach (array_merge(array_keys($this->_missingFields
), array_keys($this->_invalidMessages
)) as $rule) {
908 foreach ((array) $this->_validatorRules
[$rule][self
::FIELDS
] as $field) {
909 unset($this->_data
[$field]);
912 foreach ($this->_validFields
as $field => $value) {
913 unset($this->_data
[$field]);
917 * Anything left over in $_data is an unknown field.
919 $this->_unknownFields
= $this->_data
;
923 * @param array $validatorRule
926 protected function _validateRule(array $validatorRule)
929 * Get one or more data values from input, and check for missing fields.
930 * Apply defaults if fields are missing.
933 foreach ((array) $validatorRule[self
::FIELDS
] as $key => $field) {
934 if (array_key_exists($field, $this->_data
)) {
935 $data[$field] = $this->_data
[$field];
936 } else if (isset($validatorRule[self
::DEFAULT_VALUE
])) {
937 /** @todo according to this code default value can't be an array. It has to be reviewed */
938 if (!is_array($validatorRule[self
::DEFAULT_VALUE
])) {
939 // Default value is a scalar
940 $data[$field] = $validatorRule[self
::DEFAULT_VALUE
];
942 // Default value is an array. Search for corresponding key
943 if (isset($validatorRule[self
::DEFAULT_VALUE
][$key])) {
944 $data[$field] = $validatorRule[self
::DEFAULT_VALUE
][$key];
945 } else if ($validatorRule[self
::PRESENCE
] == self
::PRESENCE_REQUIRED
) {
946 // Default value array is provided, but it doesn't have an entry for current field
947 // and presence is required
948 $this->_missingFields
[$validatorRule[self
::RULE
]][] =
949 $this->_getMissingMessage($validatorRule[self
::RULE
], $field);
952 } else if ($validatorRule[self
::PRESENCE
] == self
::PRESENCE_REQUIRED
) {
953 $this->_missingFields
[$validatorRule[self
::RULE
]][] =
954 $this->_getMissingMessage($validatorRule[self
::RULE
], $field);
959 * If any required fields are missing, break the loop.
961 if (isset($this->_missingFields
[$validatorRule[self
::RULE
]]) && count($this->_missingFields
[$validatorRule[self
::RULE
]]) > 0) {
966 * Evaluate the inputs against the validator chain.
968 if (count((array) $validatorRule[self
::FIELDS
]) > 1) {
969 if (!$validatorRule[self
::ALLOW_EMPTY
]) {
970 $emptyFieldsFound = false;
971 $errorsList = array();
974 foreach ($data as $fieldKey => $field) {
975 $notEmptyValidator = $this->_getValidator('NotEmpty');
976 $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self
::RULE
], $fieldKey));
978 if (!$notEmptyValidator->isValid($field)) {
979 foreach ($notEmptyValidator->getMessages() as $messageKey => $message) {
980 if (!isset($messages[$messageKey])) {
981 $messages[$messageKey] = $message;
983 $messages[] = $message;
986 $errorsList[] = $notEmptyValidator->getErrors();
987 $emptyFieldsFound = true;
991 if ($emptyFieldsFound) {
992 $this->_invalidMessages
[$validatorRule[self
::RULE
]] = $messages;
993 $this->_invalidErrors
[$validatorRule[self
::RULE
]] = array_unique(call_user_func_array('array_merge', $errorsList));
998 if (!$validatorRule[self
::VALIDATOR_CHAIN
]->isValid($data)) {
999 $this->_invalidMessages
[$validatorRule[self
::RULE
]] = $validatorRule[self
::VALIDATOR_CHAIN
]->getMessages();
1000 $this->_invalidErrors
[$validatorRule[self
::RULE
]] = $validatorRule[self
::VALIDATOR_CHAIN
]->getErrors();
1003 } else if (count($data) > 0) {
1004 // $data is actually a one element array
1005 $fieldNames = array_keys($data);
1006 $fieldName = reset($fieldNames);
1007 $field = reset($data);
1010 if (!is_array($field)) {
1011 $field = array($field);
1014 $notEmptyValidator = $this->_getValidator('NotEmpty');
1015 $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self
::RULE
], $fieldName));
1016 if ($validatorRule[self
::ALLOW_EMPTY
]) {
1017 $validatorChain = $validatorRule[self
::VALIDATOR_CHAIN
];
1019 $validatorChain = new Zend_Validate();
1020 $validatorChain->addValidator($notEmptyValidator, true /* Always break on failure */);
1021 $validatorChain->addValidator($validatorRule[self
::VALIDATOR_CHAIN
]);
1024 foreach ($field as $value) {
1025 if ($validatorRule[self
::ALLOW_EMPTY
] && !$notEmptyValidator->isValid($value)) {
1026 // Field is empty AND it's allowed. Do nothing.
1030 if (!$validatorChain->isValid($value)) {
1031 if (isset($this->_invalidMessages
[$validatorRule[self
::RULE
]])) {
1032 $collectedMessages = $this->_invalidMessages
[$validatorRule[self
::RULE
]];
1034 $collectedMessages = array();
1037 foreach ($validatorChain->getMessages() as $messageKey => $message) {
1038 if (!isset($collectedMessages[$messageKey])) {
1039 $collectedMessages[$messageKey] = $message;
1041 $collectedMessages[] = $message;
1045 $this->_invalidMessages
[$validatorRule[self
::RULE
]] = $collectedMessages;
1046 if (isset($this->_invalidErrors
[$validatorRule[self
::RULE
]])) {
1047 $this->_invalidErrors
[$validatorRule[self
::RULE
]] = array_merge($this->_invalidErrors
[$validatorRule[self
::RULE
]],
1048 $validatorChain->getErrors());
1050 $this->_invalidErrors
[$validatorRule[self
::RULE
]] = $validatorChain->getErrors();
1052 unset($this->_validFields
[$fieldName]);
1054 if ($validatorRule[self
::BREAK_CHAIN
]) {
1065 * If we got this far, the inputs for this rule pass validation.
1067 foreach ((array) $validatorRule[self
::FIELDS
] as $field) {
1068 if (array_key_exists($field, $data)) {
1069 $this->_validFields
[$field] = $data[$field];
1075 * @param mixed $classBaseName
1076 * @return Zend_Filter_Interface
1078 protected function _getFilter($classBaseName)
1080 return $this->_getFilterOrValidator(self
::FILTER
, $classBaseName);
1084 * @param mixed $classBaseName
1085 * @return Zend_Validate_Interface
1087 protected function _getValidator($classBaseName)
1089 return $this->_getFilterOrValidator(self
::VALIDATE
, $classBaseName);
1093 * @param string $type
1094 * @param mixed $classBaseName
1095 * @return Zend_Filter_Interface|Zend_Validate_Interface
1096 * @throws Zend_Filter_Exception
1098 protected function _getFilterOrValidator($type, $classBaseName)
1102 if (is_array($classBaseName)) {
1103 $args = $classBaseName;
1104 $classBaseName = array_shift($args);
1107 $interfaceName = 'Zend_' . ucfirst($type) . '_Interface';
1108 $className = $this->getPluginLoader($type)->load(ucfirst($classBaseName));
1110 $class = new ReflectionClass($className);
1112 if (!$class->implementsInterface($interfaceName)) {
1113 require_once 'Zend/Filter/Exception.php';
1114 throw new Zend_Filter_Exception("Class '$className' based on basename '$classBaseName' must implement the '$interfaceName' interface");
1117 if ($class->hasMethod('__construct')) {
1118 $object = $class->newInstanceArgs($args);
1120 $object = $class->newInstance();