Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend_Filter_Input Class Reference

Public Member Functions

 __construct ($filterRules, $validatorRules, array $data=null, array $options=null)
 
 addNamespace ($namespaces)
 
 addFilterPrefixPath ($prefix, $path)
 
 addValidatorPrefixPath ($prefix, $path)
 
 setPluginLoader (Zend_Loader_PluginLoader_Interface $loader, $type)
 
 getPluginLoader ($type)
 
 getMessages ()
 
 getErrors ()
 
 getInvalid ()
 
 getMissing ()
 
 getUnknown ()
 
 getEscaped ($fieldName=null)
 
 getUnescaped ($fieldName=null)
 
 __get ($fieldName)
 
 hasInvalid ()
 
 hasMissing ()
 
 hasUnknown ()
 
 hasValid ()
 
 isValid ($fieldName=null)
 
 __isset ($fieldName)
 
 process ()
 
 setData (array $data)
 
 setDefaultEscapeFilter ($escapeFilter)
 
 setOptions (array $options)
 
 setTranslator ($translator=null)
 
 getTranslator ()
 
 setDisableTranslator ($flag)
 
 translatorIsDisabled ()
 

Data Fields

const ALLOW_EMPTY = 'allowEmpty'
 
const BREAK_CHAIN = 'breakChainOnFailure'
 
const DEFAULT_VALUE = 'default'
 
const MESSAGES = 'messages'
 
const ESCAPE_FILTER = 'escapeFilter'
 
const FIELDS = 'fields'
 
const FILTER = 'filter'
 
const FILTER_CHAIN = 'filterChain'
 
const MISSING_MESSAGE = 'missingMessage'
 
const INPUT_NAMESPACE = 'inputNamespace'
 
const VALIDATOR_NAMESPACE = 'validatorNamespace'
 
const FILTER_NAMESPACE = 'filterNamespace'
 
const NOT_EMPTY_MESSAGE = 'notEmptyMessage'
 
const PRESENCE = 'presence'
 
const PRESENCE_OPTIONAL = 'optional'
 
const PRESENCE_REQUIRED = 'required'
 
const RULE = 'rule'
 
const RULE_WILDCARD = '*'
 
const VALIDATE = 'validate'
 
const VALIDATOR = 'validator'
 
const VALIDATOR_CHAIN = 'validatorChain'
 
const VALIDATOR_CHAIN_COUNT = 'validatorChainCount'
 

Protected Member Functions

 _escapeRecursive ($data)
 
 _filter ()
 
 _filterRule (array $filterRule)
 
 _getDefaultEscapeFilter ()
 
 _getMissingMessage ($rule, $field)
 
 _getNotEmptyMessage ($rule, $field)
 
 _process ()
 
 _validate ()
 
 _validateRule (array $validatorRule)
 
 _getNotEmptyValidatorInstance ($validatorRule)
 
 _getFilter ($classBaseName)
 
 _getValidator ($classBaseName)
 
 _getFilterOrValidator ($type, $classBaseName)
 

Protected Attributes

 $_data = array()
 
 $_filterRules = array()
 
 $_validatorRules = array()
 
 $_validFields = array()
 
 $_invalidMessages = array()
 
 $_invalidErrors = array()
 
 $_missingFields = array()
 
 $_unknownFields = array()
 
 $_defaultEscapeFilter = null
 
 $_loaders = array()
 
 $_defaults
 
 $_processed = false
 
 $_translator
 
 $_translatorDisabled = false
 

Detailed Description

Definition at line 43 of file Input.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $filterRules,
  $validatorRules,
array  $data = null,
array  $options = null 
)
Parameters
array$filterRules
array$validatorRules
array$dataOPTIONAL
array$optionsOPTIONAL

Definition at line 165 of file Input.php.

166  {
167  if ($options) {
168  $this->setOptions($options);
169  }
170 
171  $this->_filterRules = (array) $filterRules;
172  $this->_validatorRules = (array) $validatorRules;
173 
174  if ($data) {
175  $this->setData($data);
176  }
177  }
setOptions(array $options)
Definition: Input.php:522
setData(array $data)
Definition: Input.php:482

Member Function Documentation

◆ __get()

__get (   $fieldName)
Parameters
string$fieldName
Returns
mixed

Definition at line 395 of file Input.php.

396  {
397  return $this->getEscaped($fieldName);
398  }
getEscaped($fieldName=null)
Definition: Input.php:342

◆ __isset()

__isset (   $fieldName)
Parameters
string$fieldName
Returns
boolean

Definition at line 453 of file Input.php.

454  {
455  $this->_process();
456  return isset($this->_validFields[$fieldName]);
457  }

◆ _escapeRecursive()

_escapeRecursive (   $data)
protected
Parameters
mixed$value
Returns
mixed

Definition at line 360 of file Input.php.

361  {
362  if($data === null) {
363  return $data;
364  }
365 
366  if (!is_array($data)) {
367  return $this->_getDefaultEscapeFilter()->filter($data);
368  }
369  foreach ($data as &$element) {
370  $element = $this->_escapeRecursive($element);
371  }
372  return $data;
373  }
_escapeRecursive($data)
Definition: Input.php:360
_getDefaultEscapeFilter()
Definition: Input.php:729
$element
Definition: element.phtml:12

◆ _filter()

_filter ( )
protected
Returns
void

Make sure we have an array representing this filter chain. Don't typecast to (array) because it might be a Zend_Filter object

Filters are indexed by integer, metacommands are indexed by string. Pick out the filters.

Use defaults for filter metacommands.

Load all the filter classes and add them to the chain.

If the ruleName is the special wildcard rule, then apply the filter chain to all input data. Else just process the field named by the rule.

Definition at line 648 of file Input.php.

649  {
650  foreach ($this->_filterRules as $ruleName => &$filterRule) {
655  if (!is_array($filterRule)) {
656  $filterRule = array($filterRule);
657  }
658 
663  $filterList = array();
664  foreach ($filterRule as $key => $value) {
665  if (is_int($key)) {
666  $filterList[] = $value;
667  }
668  }
669 
673  $filterRule[self::RULE] = $ruleName;
674  if (!isset($filterRule[self::FIELDS])) {
675  $filterRule[self::FIELDS] = $ruleName;
676  }
677 
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);
686  }
687  $filterRule[self::FILTER_CHAIN]->addFilter($filter);
688  }
689  }
690 
696  if ($ruleName == self::RULE_WILDCARD) {
697  foreach (array_keys($this->_data) as $field) {
698  $this->_filterRule(array_merge($filterRule, array(self::FIELDS => $field)));
699  }
700  } else {
701  $this->_filterRule($filterRule);
702  }
703  }
704  }
_getFilter($classBaseName)
Definition: Input.php:1161
$value
Definition: gender.phtml:16
_filterRule(array $filterRule)
Definition: Input.php:710
const FIELDS
Definition: Input.php:51
const FILTER_CHAIN
Definition: Input.php:53

◆ _filterRule()

_filterRule ( array  $filterRule)
protected
Parameters
array$filterRule
Returns
void

Definition at line 710 of file Input.php.

711  {
712  $field = $filterRule[self::FIELDS];
713  if (!array_key_exists($field, $this->_data)) {
714  return;
715  }
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);
719  }
720  } else {
721  $this->_data[$field] =
722  $filterRule[self::FILTER_CHAIN]->filter($this->_data[$field]);
723  }
724  }
$value
Definition: gender.phtml:16
const FIELDS
Definition: Input.php:51
const FILTER_CHAIN
Definition: Input.php:53

◆ _getDefaultEscapeFilter()

_getDefaultEscapeFilter ( )
protected
Returns
Zend_Filter_Interface

Definition at line 729 of file Input.php.

730  {
731  if ($this->_defaultEscapeFilter !== null) {
733  }
734  return $this->setDefaultEscapeFilter($this->_defaults[self::ESCAPE_FILTER]);
735  }
setDefaultEscapeFilter($escapeFilter)
Definition: Input.php:504

◆ _getFilter()

_getFilter (   $classBaseName)
protected
Parameters
mixed$classBaseName
Returns
Zend_Filter_Interface

Definition at line 1161 of file Input.php.

1162  {
1163  return $this->_getFilterOrValidator(self::FILTER, $classBaseName);
1164  }
_getFilterOrValidator($type, $classBaseName)
Definition: Input.php:1181

◆ _getFilterOrValidator()

_getFilterOrValidator (   $type,
  $classBaseName 
)
protected
Parameters
string$type
mixed$classBaseName
Returns
Zend_Filter_Interface|Zend_Validate_Interface
Exceptions
Zend_Filter_Exception

Definition at line 1181 of file Input.php.

1182  {
1183  $args = array();
1184 
1185  if (is_array($classBaseName)) {
1186  $args = $classBaseName;
1187  $classBaseName = array_shift($args);
1188  }
1189 
1190  $interfaceName = 'Zend_' . ucfirst($type) . '_Interface';
1191  $className = $this->getPluginLoader($type)->load(ucfirst($classBaseName));
1192 
1193  $class = new ReflectionClass($className);
1194 
1195  if (!$class->implementsInterface($interfaceName)) {
1196  #require_once 'Zend/Filter/Exception.php';
1197  throw new Zend_Filter_Exception("Class '$className' based on basename '$classBaseName' must implement the '$interfaceName' interface");
1198  }
1199 
1200  if ($class->hasMethod('__construct')) {
1201  $object = $class->newInstanceArgs($args);
1202  } else {
1203  $object = $class->newInstance();
1204  }
1205 
1206  return $object;
1207  }
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
getPluginLoader($type)
Definition: Input.php:266
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31

◆ _getMissingMessage()

_getMissingMessage (   $rule,
  $field 
)
protected
Parameters
string$rule
string$field
Returns
string

Definition at line 742 of file Input.php.

743  {
744  $message = $this->_defaults[self::MISSING_MESSAGE];
745 
746  if (null !== ($translator = $this->getTranslator())) {
747  if ($translator->isTranslated(self::MISSING_MESSAGE)) {
748  $message = $translator->translate(self::MISSING_MESSAGE);
749  } else {
750  $message = $translator->translate($message);
751  }
752  }
753 
754  $message = str_replace('%rule%', $rule, $message);
755  $message = str_replace('%field%', $field, $message);
756  return $message;
757  }
$message
const MISSING_MESSAGE
Definition: Input.php:54

◆ _getNotEmptyMessage()

_getNotEmptyMessage (   $rule,
  $field 
)
protected
Returns
string

Definition at line 762 of file Input.php.

763  {
764  $message = $this->_defaults[self::NOT_EMPTY_MESSAGE];
765 
766  if (null !== ($translator = $this->getTranslator())) {
767  if ($translator->isTranslated(self::NOT_EMPTY_MESSAGE)) {
768  $message = $translator->translate(self::NOT_EMPTY_MESSAGE);
769  } else {
770  $message = $translator->translate($message);
771  }
772  }
773 
774  $message = str_replace('%rule%', $rule, $message);
775  $message = str_replace('%field%', $field, $message);
776  return $message;
777  }
const NOT_EMPTY_MESSAGE
Definition: Input.php:58
$message

◆ _getNotEmptyValidatorInstance()

_getNotEmptyValidatorInstance (   $validatorRule)
protected

Check a validatorRule for the presence of a NotEmpty validator instance. The purpose is to preserve things like a custom message, that may have been set on the validator outside Zend_Filter_Input.

Parameters
array$validatorRule
Returns
mixed false if none is found, Zend_Validate_NotEmpty instance if found

Definition at line 1147 of file Input.php.

1147  {
1148  foreach ($validatorRule as $rule => $value) {
1149  if (is_object($value) and $value instanceof Zend_Validate_NotEmpty) {
1150  return $value;
1151  }
1152  }
1153 
1154  return false;
1155  }
$value
Definition: gender.phtml:16

◆ _getValidator()

_getValidator (   $classBaseName)
protected
Parameters
mixed$classBaseName
Returns
Zend_Validate_Interface

Definition at line 1170 of file Input.php.

1171  {
1172  return $this->_getFilterOrValidator(self::VALIDATE, $classBaseName);
1173  }
_getFilterOrValidator($type, $classBaseName)
Definition: Input.php:1181

◆ _process()

_process ( )
protected
Returns
void

Definition at line 782 of file Input.php.

783  {
784  if ($this->_processed === false) {
785  $this->_filter();
786  $this->_validate();
787  $this->_processed = true;
788  }
789  }

◆ _validate()

_validate ( )
protected
Returns
void

Special case: if there are no validators, treat all fields as valid.

Make sure we have an array representing this validator chain. Don't typecast to (array) because it might be a Zend_Validate object

Validators are indexed by integer, metacommands are indexed by string. Pick out the validators.

Use defaults for validation metacommands.

Load all the validator classes and add them to the chain.

we are changing the defaults here, this is alright if all subsequent validators are also a not empty validator, but it goes wrong if one of them is not AND is required!!! that is why we restore the default value at the end of this loop

If the ruleName is the special wildcard rule, then apply the validator chain to all input data. Else just process the field named by the rule.

Unset fields in $_data that have been added to other arrays. We have to wait until all rules have been processed because a given field may be referenced by multiple rules.

Anything left over in $_data is an unknown field.

Definition at line 794 of file Input.php.

795  {
799  if (!$this->_validatorRules) {
800  $this->_validFields = $this->_data;
801  $this->_data = array();
802  return;
803  }
804 
805  // remember the default not empty message in case we want to temporarily change it
806  $preserveDefaultNotEmptyMessage = $this->_defaults[self::NOT_EMPTY_MESSAGE];
807 
808  foreach ($this->_validatorRules as $ruleName => &$validatorRule) {
813  if (!is_array($validatorRule)) {
814  $validatorRule = array($validatorRule);
815  }
816 
821  $validatorList = array();
822  foreach ($validatorRule as $key => $value) {
823  if (is_int($key)) {
824  $validatorList[$key] = $value;
825  }
826  }
827 
831  $validatorRule[self::RULE] = $ruleName;
832  if (!isset($validatorRule[self::FIELDS])) {
833  $validatorRule[self::FIELDS] = $ruleName;
834  }
835  if (!isset($validatorRule[self::BREAK_CHAIN])) {
836  $validatorRule[self::BREAK_CHAIN] = $this->_defaults[self::BREAK_CHAIN];
837  }
838  if (!isset($validatorRule[self::PRESENCE])) {
839  $validatorRule[self::PRESENCE] = $this->_defaults[self::PRESENCE];
840  }
841  if (!isset($validatorRule[self::ALLOW_EMPTY])) {
842  $foundNotEmptyValidator = false;
843 
844  foreach ($validatorRule as $rule) {
845  if ($rule === 'NotEmpty') {
846  $foundNotEmptyValidator = true;
847  // field may not be empty, we are ready
848  break 1;
849  }
850 
851  if (is_array($rule)) {
852  $keys = array_keys($rule);
853  $classKey = array_shift($keys);
854  if (isset($rule[$classKey])) {
855  $ruleClass = $rule[$classKey];
856  if ($ruleClass === 'NotEmpty') {
857  $foundNotEmptyValidator = true;
858  // field may not be empty, we are ready
859  break 1;
860  }
861  }
862  }
863 
864  // we must check if it is an object before using instanceof
865  if (!is_object($rule)) {
866  // it cannot be a NotEmpty validator, skip this one
867  continue;
868  }
869 
870  if($rule instanceof Zend_Validate_NotEmpty) {
871  $foundNotEmptyValidator = true;
872  // field may not be empty, we are ready
873  break 1;
874  }
875  }
876 
877  if (!$foundNotEmptyValidator) {
878  $validatorRule[self::ALLOW_EMPTY] = $this->_defaults[self::ALLOW_EMPTY];
879  } else {
880  $validatorRule[self::ALLOW_EMPTY] = false;
881  }
882  }
883 
884  if (!isset($validatorRule[self::MESSAGES])) {
885  $validatorRule[self::MESSAGES] = array();
886  } else if (!is_array($validatorRule[self::MESSAGES])) {
887  $validatorRule[self::MESSAGES] = array($validatorRule[self::MESSAGES]);
888  } else if (array_intersect_key($validatorList, $validatorRule[self::MESSAGES])) {
889  // this seems pointless... it just re-adds what it already has...
890  // I can disable all this and not a single unit test fails...
891  // There are now corresponding numeric keys in the validation rule messages array
892  // Treat it as a named messages list for all rule validators
893  $unifiedMessages = $validatorRule[self::MESSAGES];
894  $validatorRule[self::MESSAGES] = array();
895 
896  foreach ($validatorList as $key => $validator) {
897  if (array_key_exists($key, $unifiedMessages)) {
898  $validatorRule[self::MESSAGES][$key] = $unifiedMessages[$key];
899  }
900  }
901  }
902 
906  if (!isset($validatorRule[self::VALIDATOR_CHAIN])) {
907  $validatorRule[self::VALIDATOR_CHAIN] = new Zend_Validate();
908 
909  foreach ($validatorList as $key => $validator) {
910  if (is_string($validator) || is_array($validator)) {
911  $validator = $this->_getValidator($validator);
912  }
913 
914  if (isset($validatorRule[self::MESSAGES][$key])) {
915  $value = $validatorRule[self::MESSAGES][$key];
916  if (is_array($value)) {
917  $validator->setMessages($value);
918  } else {
919  $validator->setMessage($value);
920  }
921 
922  if ($validator instanceof Zend_Validate_NotEmpty) {
927  if (is_array($value)) {
928  $temp = $value; // keep the original value
929  $this->_defaults[self::NOT_EMPTY_MESSAGE] = array_pop($temp);
930  unset($temp);
931  } else {
932  $this->_defaults[self::NOT_EMPTY_MESSAGE] = $value;
933  }
934  }
935  }
936 
937  $validatorRule[self::VALIDATOR_CHAIN]->addValidator($validator, $validatorRule[self::BREAK_CHAIN]);
938  }
939  $validatorRule[self::VALIDATOR_CHAIN_COUNT] = count($validatorList);
940  }
941 
947  if ($ruleName == self::RULE_WILDCARD) {
948  foreach (array_keys($this->_data) as $field) {
949  $this->_validateRule(array_merge($validatorRule, array(self::FIELDS => $field)));
950  }
951  } else {
952  $this->_validateRule($validatorRule);
953  }
954 
955  // reset the default not empty message
956  $this->_defaults[self::NOT_EMPTY_MESSAGE] = $preserveDefaultNotEmptyMessage;
957  }
958 
959 
960 
966  foreach (array_merge(array_keys($this->_missingFields), array_keys($this->_invalidMessages)) as $rule) {
967  foreach ((array) $this->_validatorRules[$rule][self::FIELDS] as $field) {
968  unset($this->_data[$field]);
969  }
970  }
971  foreach ($this->_validFields as $field => $value) {
972  unset($this->_data[$field]);
973  }
974 
978  $this->_unknownFields = $this->_data;
979  }
const VALIDATOR_CHAIN
Definition: Input.php:66
const NOT_EMPTY_MESSAGE
Definition: Input.php:58
const VALIDATOR_CHAIN_COUNT
Definition: Input.php:67
const PRESENCE
Definition: Input.php:59
_validateRule(array $validatorRule)
Definition: Input.php:985
$value
Definition: gender.phtml:16
_getValidator($classBaseName)
Definition: Input.php:1170
const MESSAGES
Definition: Input.php:49
const FIELDS
Definition: Input.php:51
const BREAK_CHAIN
Definition: Input.php:47
const ALLOW_EMPTY
Definition: Input.php:46

◆ _validateRule()

_validateRule ( array  $validatorRule)
protected
Parameters
array$validatorRule
Returns
void

Get one or more data values from input, and check for missing fields. Apply defaults if fields are missing.

Todo:
according to this code default value can't be an array. It has to be reviewed

If any required fields are missing, break the loop.

Evaluate the inputs against the validator chain.

If we got this far, the inputs for this rule pass validation.

Definition at line 985 of file Input.php.

986  {
991  $data = array();
992  foreach ((array) $validatorRule[self::FIELDS] as $key => $field) {
993  if (array_key_exists($field, $this->_data)) {
994  $data[$field] = $this->_data[$field];
995  } else if (isset($validatorRule[self::DEFAULT_VALUE])) {
997  if (!is_array($validatorRule[self::DEFAULT_VALUE])) {
998  // Default value is a scalar
999  $data[$field] = $validatorRule[self::DEFAULT_VALUE];
1000  } else {
1001  // Default value is an array. Search for corresponding key
1002  if (isset($validatorRule[self::DEFAULT_VALUE][$key])) {
1003  $data[$field] = $validatorRule[self::DEFAULT_VALUE][$key];
1004  } else if ($validatorRule[self::PRESENCE] == self::PRESENCE_REQUIRED) {
1005  // Default value array is provided, but it doesn't have an entry for current field
1006  // and presence is required
1007  $this->_missingFields[$validatorRule[self::RULE]][] =
1008  $this->_getMissingMessage($validatorRule[self::RULE], $field);
1009  }
1010  }
1011  } else if ($validatorRule[self::PRESENCE] == self::PRESENCE_REQUIRED) {
1012  $this->_missingFields[$validatorRule[self::RULE]][] =
1013  $this->_getMissingMessage($validatorRule[self::RULE], $field);
1014  }
1015  }
1016 
1020  if (isset($this->_missingFields[$validatorRule[self::RULE]]) && count($this->_missingFields[$validatorRule[self::RULE]]) > 0) {
1021  return;
1022  }
1023 
1027  if (count((array) $validatorRule[self::FIELDS]) > 1) {
1028  if (!$validatorRule[self::ALLOW_EMPTY]) {
1029  $emptyFieldsFound = false;
1030  $errorsList = array();
1031  $messages = array();
1032 
1033  foreach ($data as $fieldKey => $field) {
1034  // if there is no Zend_Validate_NotEmpty instance in the rules, we will use the default
1035  if (!($notEmptyValidator = $this->_getNotEmptyValidatorInstance($validatorRule))) {
1036  $notEmptyValidator = $this->_getValidator('NotEmpty');
1037  $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldKey));
1038  }
1039 
1040  if (!$notEmptyValidator->isValid($field)) {
1041  foreach ($notEmptyValidator->getMessages() as $messageKey => $message) {
1042  if (!isset($messages[$messageKey])) {
1043  $messages[$messageKey] = $message;
1044  } else {
1045  $messages[] = $message;
1046  }
1047  }
1048  $errorsList[] = $notEmptyValidator->getErrors();
1049  $emptyFieldsFound = true;
1050  }
1051  }
1052 
1053  if ($emptyFieldsFound) {
1054  $this->_invalidMessages[$validatorRule[self::RULE]] = $messages;
1055  $this->_invalidErrors[$validatorRule[self::RULE]] = array_unique(call_user_func_array('array_merge', $errorsList));
1056  return;
1057  }
1058  }
1059 
1060  if (!$validatorRule[self::VALIDATOR_CHAIN]->isValid($data)) {
1061  $this->_invalidMessages[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getMessages();
1062  $this->_invalidErrors[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getErrors();
1063  return;
1064  }
1065  } else if (count($data) > 0) {
1066  // $data is actually a one element array
1067  $fieldNames = array_keys($data);
1068  $fieldName = reset($fieldNames);
1069  $field = reset($data);
1070 
1071  $failed = false;
1072  if (!is_array($field)) {
1073  $field = array($field);
1074  }
1075 
1076  // if there is no Zend_Validate_NotEmpty instance in the rules, we will use the default
1077  if (!($notEmptyValidator = $this->_getNotEmptyValidatorInstance($validatorRule))) {
1078  $notEmptyValidator = $this->_getValidator('NotEmpty');
1079  $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldName));
1080  }
1081 
1082  if ($validatorRule[self::ALLOW_EMPTY]) {
1083  $validatorChain = $validatorRule[self::VALIDATOR_CHAIN];
1084  } else {
1085  $validatorChain = new Zend_Validate();
1086  $validatorChain->addValidator($notEmptyValidator, true /* Always break on failure */);
1087  $validatorChain->addValidator($validatorRule[self::VALIDATOR_CHAIN]);
1088  }
1089 
1090  foreach ($field as $key => $value) {
1091  if ($validatorRule[self::ALLOW_EMPTY] && !$notEmptyValidator->isValid($value)) {
1092  // Field is empty AND it's allowed. Do nothing.
1093  continue;
1094  }
1095 
1096  if (!$validatorChain->isValid($value)) {
1097  if (isset($this->_invalidMessages[$validatorRule[self::RULE]])) {
1098  $collectedMessages = $this->_invalidMessages[$validatorRule[self::RULE]];
1099  } else {
1100  $collectedMessages = array();
1101  }
1102 
1103  foreach ($validatorChain->getMessages() as $messageKey => $message) {
1104  if (!isset($collectedMessages[$messageKey])) {
1105  $collectedMessages[$messageKey] = $message;
1106  } else {
1107  $collectedMessages[] = $message;
1108  }
1109  }
1110 
1111  $this->_invalidMessages[$validatorRule[self::RULE]] = $collectedMessages;
1112  if (isset($this->_invalidErrors[$validatorRule[self::RULE]])) {
1113  $this->_invalidErrors[$validatorRule[self::RULE]] = array_merge($this->_invalidErrors[$validatorRule[self::RULE]],
1114  $validatorChain->getErrors());
1115  } else {
1116  $this->_invalidErrors[$validatorRule[self::RULE]] = $validatorChain->getErrors();
1117  }
1118  unset($this->_validFields[$fieldName]);
1119  $failed = true;
1120  if ($validatorRule[self::BREAK_CHAIN]) {
1121  return;
1122  }
1123  }
1124  }
1125  if ($failed) {
1126  return;
1127  }
1128  }
1129 
1133  foreach ((array) $validatorRule[self::FIELDS] as $field) {
1134  if (array_key_exists($field, $data)) {
1135  $this->_validFields[$field] = $data[$field];
1136  }
1137  }
1138  }
const VALIDATOR_CHAIN
Definition: Input.php:66
$message
$value
Definition: gender.phtml:16
_getValidator($classBaseName)
Definition: Input.php:1170
const DEFAULT_VALUE
Definition: Input.php:48
_getMissingMessage($rule, $field)
Definition: Input.php:742
_getNotEmptyMessage($rule, $field)
Definition: Input.php:762
isValid($fieldName=null)
Definition: Input.php:440
_getNotEmptyValidatorInstance($validatorRule)
Definition: Input.php:1147

◆ addFilterPrefixPath()

addFilterPrefixPath (   $prefix,
  $path 
)

Add prefix path for all elements

Parameters
string$prefix
string$path
Returns
Zend_Filter_Input

Definition at line 207 of file Input.php.

208  {
209  $this->getPluginLoader(self::FILTER)->addPrefixPath($prefix, $path);
210 
211  return $this;
212  }
$prefix
Definition: name.phtml:25
getPluginLoader($type)
Definition: Input.php:266

◆ addNamespace()

addNamespace (   $namespaces)
Parameters
mixed$namespaces
Returns
Zend_Filter_Input
Deprecated:
since 1.5.0RC1 - use addFilterPrefixPath() or addValidatorPrefixPath instead.

Definition at line 184 of file Input.php.

185  {
186  if (!is_array($namespaces)) {
187  $namespaces = array($namespaces);
188  }
189 
190  foreach ($namespaces as $namespace) {
191  $prefix = $namespace;
192  $path = str_replace('_', DIRECTORY_SEPARATOR, $prefix);
195  }
196 
197  return $this;
198  }
addValidatorPrefixPath($prefix, $path)
Definition: Input.php:221
addFilterPrefixPath($prefix, $path)
Definition: Input.php:207
$prefix
Definition: name.phtml:25

◆ addValidatorPrefixPath()

addValidatorPrefixPath (   $prefix,
  $path 
)

Add prefix path for all elements

Parameters
string$prefix
string$path
Returns
Zend_Filter_Input

Definition at line 221 of file Input.php.

222  {
223  $this->getPluginLoader(self::VALIDATE)->addPrefixPath($prefix, $path);
224 
225  return $this;
226  }
$prefix
Definition: name.phtml:25
getPluginLoader($type)
Definition: Input.php:266

◆ getErrors()

getErrors ( )
Returns
array

Definition at line 305 of file Input.php.

306  {
307  $this->_process();
308  return $this->_invalidErrors;
309  }

◆ getEscaped()

getEscaped (   $fieldName = null)
Parameters
string$fieldNameOPTIONAL
Returns
mixed

Definition at line 342 of file Input.php.

343  {
344  $this->_process();
345  $this->_getDefaultEscapeFilter();
346 
347  if ($fieldName === null) {
348  return $this->_escapeRecursive($this->_validFields);
349  }
350  if (array_key_exists($fieldName, $this->_validFields)) {
351  return $this->_escapeRecursive($this->_validFields[$fieldName]);
352  }
353  return null;
354  }
_escapeRecursive($data)
Definition: Input.php:360
_getDefaultEscapeFilter()
Definition: Input.php:729

◆ getInvalid()

getInvalid ( )
Returns
array

Definition at line 314 of file Input.php.

315  {
316  $this->_process();
318  }

◆ getMessages()

getMessages ( )
Returns
array

Definition at line 296 of file Input.php.

297  {
298  $this->_process();
299  return array_merge($this->_invalidMessages, $this->_missingFields);
300  }

◆ getMissing()

getMissing ( )
Returns
array

Definition at line 323 of file Input.php.

324  {
325  $this->_process();
326  return $this->_missingFields;
327  }

◆ getPluginLoader()

getPluginLoader (   $type)

Retrieve plugin loader for given type

$type may be one of:

  • filter
  • validator

If a plugin loader does not exist for the given type, defaults are created.

Parameters
string$type'filter' or 'validate'
Returns
Zend_Loader_PluginLoader_Interface
Exceptions
Zend_Filter_Exceptionon invalid type

Definition at line 266 of file Input.php.

267  {
268  $type = strtolower($type);
269  if (!isset($this->_loaders[$type])) {
270  switch ($type) {
271  case self::FILTER:
272  $prefixSegment = 'Zend_Filter_';
273  $pathSegment = 'Zend/Filter/';
274  break;
275  case self::VALIDATE:
276  $prefixSegment = 'Zend_Validate_';
277  $pathSegment = 'Zend/Validate/';
278  break;
279  default:
280  #require_once 'Zend/Filter/Exception.php';
281  throw new Zend_Filter_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
282  }
283 
284  #require_once 'Zend/Loader/PluginLoader.php';
285  $this->_loaders[$type] = new Zend_Loader_PluginLoader(
286  array($prefixSegment => $pathSegment)
287  );
288  }
289 
290  return $this->_loaders[$type];
291  }
$type
Definition: item.phtml:13
const FILTER
Definition: Input.php:52
const VALIDATE
Definition: Input.php:64

◆ getTranslator()

getTranslator ( )

Return translation object

Returns
Zend_Translate_Adapter|null

Definition at line 598 of file Input.php.

599  {
600  if ($this->translatorIsDisabled()) {
601  return null;
602  }
603 
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) {
609  return $translator;
610  } elseif ($translator instanceof Zend_Translate) {
611  return $translator->getAdapter();
612  }
613  }
614  }
615 
616  return $this->_translator;
617  }
translatorIsDisabled()
Definition: Input.php:636
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static isRegistered($index)
Definition: Registry.php:178
static get($index)
Definition: Registry.php:141

◆ getUnescaped()

getUnescaped (   $fieldName = null)
Parameters
string$fieldNameOPTIONAL
Returns
mixed

Definition at line 379 of file Input.php.

380  {
381  $this->_process();
382  if ($fieldName === null) {
383  return $this->_validFields;
384  }
385  if (array_key_exists($fieldName, $this->_validFields)) {
386  return $this->_validFields[$fieldName];
387  }
388  return null;
389  }

◆ getUnknown()

getUnknown ( )
Returns
array

Definition at line 332 of file Input.php.

333  {
334  $this->_process();
335  return $this->_unknownFields;
336  }

◆ hasInvalid()

hasInvalid ( )
Returns
boolean

Definition at line 403 of file Input.php.

404  {
405  $this->_process();
406  return !(empty($this->_invalidMessages));
407  }

◆ hasMissing()

hasMissing ( )
Returns
boolean

Definition at line 412 of file Input.php.

413  {
414  $this->_process();
415  return !(empty($this->_missingFields));
416  }

◆ hasUnknown()

hasUnknown ( )
Returns
boolean

Definition at line 421 of file Input.php.

422  {
423  $this->_process();
424  return !(empty($this->_unknownFields));
425  }

◆ hasValid()

hasValid ( )
Returns
boolean

Definition at line 430 of file Input.php.

431  {
432  $this->_process();
433  return !(empty($this->_validFields));
434  }

◆ isValid()

isValid (   $fieldName = null)
Parameters
string$fieldName
Returns
boolean

Definition at line 440 of file Input.php.

441  {
442  $this->_process();
443  if ($fieldName === null) {
444  return !($this->hasMissing() || $this->hasInvalid());
445  }
446  return array_key_exists($fieldName, $this->_validFields);
447  }

◆ process()

process ( )
Returns
Zend_Filter_Input
Exceptions
Zend_Filter_Exception

Definition at line 463 of file Input.php.

464  {
465  $this->_process();
466  if ($this->hasInvalid()) {
467  #require_once 'Zend/Filter/Exception.php';
468  throw new Zend_Filter_Exception("Input has invalid fields");
469  }
470  if ($this->hasMissing()) {
471  #require_once 'Zend/Filter/Exception.php';
472  throw new Zend_Filter_Exception("Input has missing fields");
473  }
474 
475  return $this;
476  }

◆ setData()

setData ( array  $data)
Parameters
array$data
Returns
Zend_Filter_Input

Reset to initial state

Definition at line 482 of file Input.php.

483  {
484  $this->_data = $data;
485 
489  $this->_validFields = array();
490  $this->_invalidMessages = array();
491  $this->_invalidErrors = array();
492  $this->_missingFields = array();
493  $this->_unknownFields = array();
494 
495  $this->_processed = false;
496 
497  return $this;
498  }

◆ setDefaultEscapeFilter()

setDefaultEscapeFilter (   $escapeFilter)
Parameters
mixed$escapeFilter
Returns
Zend_Filter_Interface

Definition at line 504 of file Input.php.

505  {
506  if (is_string($escapeFilter) || is_array($escapeFilter)) {
507  $escapeFilter = $this->_getFilter($escapeFilter);
508  }
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');
512  }
513  $this->_defaultEscapeFilter = $escapeFilter;
514  return $escapeFilter;
515  }
_getFilter($classBaseName)
Definition: Input.php:1161

◆ setDisableTranslator()

setDisableTranslator (   $flag)

Indicate whether or not translation should be disabled

Parameters
bool$flag
Returns
Zend_Filter_Input

Definition at line 625 of file Input.php.

626  {
627  $this->_translatorDisabled = (bool) $flag;
628  return $this;
629  }

◆ setOptions()

setOptions ( array  $options)
Parameters
array$options
Returns
Zend_Filter_Input
Exceptions
Zend_Filter_Exceptionif an unknown option is given

Definition at line 522 of file Input.php.

523  {
524  foreach ($options as $option => $value) {
525  switch ($option) {
526  case self::ESCAPE_FILTER:
528  break;
530  $this->addNamespace($value);
531  break;
533  if(is_string($value)) {
534  $value = array($value);
535  }
536 
537  foreach($value AS $prefix) {
538  $this->addValidatorPrefixPath(
539  $prefix,
540  str_replace('_', DIRECTORY_SEPARATOR, $prefix)
541  );
542  }
543  break;
545  if(is_string($value)) {
546  $value = array($value);
547  }
548 
549  foreach($value AS $prefix) {
550  $this->addFilterPrefixPath(
551  $prefix,
552  str_replace('_', DIRECTORY_SEPARATOR, $prefix)
553  );
554  }
555  break;
556  case self::ALLOW_EMPTY:
557  case self::BREAK_CHAIN:
560  case self::PRESENCE:
561  $this->_defaults[$option] = $value;
562  break;
563  default:
564  #require_once 'Zend/Filter/Exception.php';
565  throw new Zend_Filter_Exception("Unknown option '$option'");
566  break;
567  }
568  }
569 
570  return $this;
571  }
addValidatorPrefixPath($prefix, $path)
Definition: Input.php:221
const NOT_EMPTY_MESSAGE
Definition: Input.php:58
const PRESENCE
Definition: Input.php:59
addNamespace($namespaces)
Definition: Input.php:184
const INPUT_NAMESPACE
Definition: Input.php:55
const MISSING_MESSAGE
Definition: Input.php:54
addFilterPrefixPath($prefix, $path)
Definition: Input.php:207
$prefix
Definition: name.phtml:25
$value
Definition: gender.phtml:16
const BREAK_CHAIN
Definition: Input.php:47
const ESCAPE_FILTER
Definition: Input.php:50
const FILTER_NAMESPACE
Definition: Input.php:57
const VALIDATOR_NAMESPACE
Definition: Input.php:56
setDefaultEscapeFilter($escapeFilter)
Definition: Input.php:504
const ALLOW_EMPTY
Definition: Input.php:46

◆ setPluginLoader()

setPluginLoader ( Zend_Loader_PluginLoader_Interface  $loader,
  $type 
)

Set plugin loaders for use with decorators and elements

Parameters
Zend_Loader_PluginLoader_Interface$loader
string$type'filter' or 'validate'
Returns
Zend_Filter_Input
Exceptions
Zend_Filter_Exceptionon invalid type

Definition at line 236 of file Input.php.

237  {
238  $type = strtolower($type);
239  switch ($type) {
240  case self::FILTER:
241  case self::VALIDATE:
242  $this->_loaders[$type] = $loader;
243  return $this;
244  default:
245  #require_once 'Zend/Filter/Exception.php';
246  throw new Zend_Filter_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
247  }
248 
249  return $this;
250  }
$loader
Definition: autoload.php:8
$type
Definition: item.phtml:13
const FILTER
Definition: Input.php:52
const VALIDATE
Definition: Input.php:64

◆ setTranslator()

setTranslator (   $translator = null)

Set translation object

Parameters
Zend_Translate | Zend_Translate_Adapter | null$translator
Returns
Zend_Filter_Input

Definition at line 579 of file Input.php.

580  {
581  if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
582  $this->_translator = $translator;
583  } elseif ($translator instanceof Zend_Translate) {
584  $this->_translator = $translator->getAdapter();
585  } else {
586  #require_once 'Zend/Validate/Exception.php';
587  throw new Zend_Validate_Exception('Invalid translator specified');
588  }
589 
590  return $this;
591  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ translatorIsDisabled()

translatorIsDisabled ( )

Is translation disabled?

Returns
bool

Definition at line 636 of file Input.php.

637  {
639  }

Field Documentation

◆ $_data

$_data = array()
protected

Definition at line 72 of file Input.php.

◆ $_defaultEscapeFilter

$_defaultEscapeFilter = null
protected

Definition at line 121 of file Input.php.

◆ $_defaults

$_defaults
protected
Initial value:
= array(
self::ALLOW_EMPTY => false,
self::BREAK_CHAIN => false,
self::ESCAPE_FILTER => 'HtmlEntities',
self::MISSING_MESSAGE => "Field '%field%' is required by rule '%rule%', but the field is missing",
self::NOT_EMPTY_MESSAGE => "You must give a non-empty value for field '%field%'",
self::PRESENCE => self::PRESENCE_OPTIONAL
)

Definition at line 132 of file Input.php.

◆ $_filterRules

$_filterRules = array()
protected

Definition at line 77 of file Input.php.

◆ $_invalidErrors

$_invalidErrors = array()
protected

Definition at line 102 of file Input.php.

◆ $_invalidMessages

$_invalidMessages = array()
protected

Definition at line 95 of file Input.php.

◆ $_loaders

$_loaders = array()
protected

Definition at line 127 of file Input.php.

◆ $_missingFields

$_missingFields = array()
protected

Definition at line 109 of file Input.php.

◆ $_processed

$_processed = false
protected

Definition at line 145 of file Input.php.

◆ $_translator

$_translator
protected

Definition at line 151 of file Input.php.

◆ $_translatorDisabled

$_translatorDisabled = false
protected

Definition at line 157 of file Input.php.

◆ $_unknownFields

$_unknownFields = array()
protected

Definition at line 115 of file Input.php.

◆ $_validatorRules

$_validatorRules = array()
protected

Definition at line 82 of file Input.php.

◆ $_validFields

$_validFields = array()
protected

Definition at line 88 of file Input.php.

◆ ALLOW_EMPTY

const ALLOW_EMPTY = 'allowEmpty'

Definition at line 46 of file Input.php.

◆ BREAK_CHAIN

const BREAK_CHAIN = 'breakChainOnFailure'

Definition at line 47 of file Input.php.

◆ DEFAULT_VALUE

const DEFAULT_VALUE = 'default'

Definition at line 48 of file Input.php.

◆ ESCAPE_FILTER

const ESCAPE_FILTER = 'escapeFilter'

Definition at line 50 of file Input.php.

◆ FIELDS

const FIELDS = 'fields'

Definition at line 51 of file Input.php.

◆ FILTER

const FILTER = 'filter'

Definition at line 52 of file Input.php.

◆ FILTER_CHAIN

const FILTER_CHAIN = 'filterChain'

Definition at line 53 of file Input.php.

◆ FILTER_NAMESPACE

const FILTER_NAMESPACE = 'filterNamespace'

Definition at line 57 of file Input.php.

◆ INPUT_NAMESPACE

const INPUT_NAMESPACE = 'inputNamespace'

Definition at line 55 of file Input.php.

◆ MESSAGES

const MESSAGES = 'messages'

Definition at line 49 of file Input.php.

◆ MISSING_MESSAGE

const MISSING_MESSAGE = 'missingMessage'

Definition at line 54 of file Input.php.

◆ NOT_EMPTY_MESSAGE

const NOT_EMPTY_MESSAGE = 'notEmptyMessage'

Definition at line 58 of file Input.php.

◆ PRESENCE

const PRESENCE = 'presence'

Definition at line 59 of file Input.php.

◆ PRESENCE_OPTIONAL

const PRESENCE_OPTIONAL = 'optional'

Definition at line 60 of file Input.php.

◆ PRESENCE_REQUIRED

const PRESENCE_REQUIRED = 'required'

Definition at line 61 of file Input.php.

◆ RULE

const RULE = 'rule'

Definition at line 62 of file Input.php.

◆ RULE_WILDCARD

const RULE_WILDCARD = '*'

Definition at line 63 of file Input.php.

◆ VALIDATE

const VALIDATE = 'validate'

Definition at line 64 of file Input.php.

◆ VALIDATOR

const VALIDATOR = 'validator'

Definition at line 65 of file Input.php.

◆ VALIDATOR_CHAIN

const VALIDATOR_CHAIN = 'validatorChain'

Definition at line 66 of file Input.php.

◆ VALIDATOR_CHAIN_COUNT

const VALIDATOR_CHAIN_COUNT = 'validatorChainCount'

Definition at line 67 of file Input.php.

◆ VALIDATOR_NAMESPACE

const VALIDATOR_NAMESPACE = 'validatorNamespace'

Definition at line 56 of file Input.php.


The documentation for this class was generated from the following file: