TYPO3 CMS  TYPO3_6-2
ValidatorUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Form\Utility;
3 
23 
29  protected $rules = array();
30 
36  protected $messages = array();
37 
43  protected $errors = array();
44 
48  public function __construct() {}
49 
56  protected function getPrefix() {
57  return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Request')->getPrefix();
58  }
59 
68  public function createRule($class, $arguments = array()) {
69  $class = strtolower((string) $class);
70  $className = 'TYPO3\\CMS\\Form\\Validation\\' . ucfirst($class) . 'Validator';
71  $rule = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className, $arguments);
72  return $rule;
73  }
74 
84  public function addRule($rule, $fieldName, $breakOnError = FALSE) {
85  $prefix = $this->getPrefix();
86  $this->rules[$prefix][] = array(
87  'instance' => (object) $rule,
88  'fieldName' => (string) $fieldName,
89  'breakOnError' => (bool) $breakOnError
90  );
91  if ($rule->messageMustBeDisplayed()) {
92  if (!isset($this->messages[$prefix][$fieldName])) {
93  $this->messages[$prefix][$fieldName] = array();
94  }
95  end($this->rules[$prefix]);
96  $key = key($this->rules[$prefix]);
97  $message = $rule->getMessage();
98  $this->messages[$prefix][$fieldName][$key][$key + 1] = $message['cObj'];
99  $this->messages[$prefix][$fieldName][$key][($key + 1) . '.'] = $message['cObj.'];
100  }
101  return $this;
102  }
103 
111  public function isValid() {
112  $prefix = $this->getPrefix();
113  $this->errors[$prefix] = array();
114  $result = TRUE;
115  if (is_array($this->rules[$prefix])) {
116  foreach ($this->rules[$prefix] as $key => $element) {
117  $rule = $element['instance'];
118  $fieldName = $element['fieldName'];
119  if ($rule->isValid()) {
120  continue;
121  }
122  $result = FALSE;
123  if (!isset($this->errors[$prefix][$fieldName])) {
124  $this->errors[$prefix][$fieldName] = array();
125  }
126  $error = $rule->getError();
127  $this->errors[$prefix][$fieldName][$key][$key + 1] = $error['cObj'];
128  $this->errors[$prefix][$fieldName][$key][($key + 1) . '.'] = $error['cObj.'];
129  if ($element['breakOnError']) {
130  break;
131  }
132  }
133  }
134  return $result;
135  }
136 
142  public function getMessages() {
143  return $this->messages[$this->getPrefix()];
144  }
145 
152  public function getMessagesByName($name) {
153  return $this->messages[$this->getPrefix()][$name];
154  }
155 
162  public function hasMessage($name) {
163  if (isset($this->messages[$this->getPrefix()][$name])) {
164  return TRUE;
165  }
166  return FALSE;
167  }
168 
174  public function getErrors() {
175  return $this->errors[$this->getPrefix()];
176  }
177 
184  public function getErrorsByName($name) {
185  return $this->errors[$this->getPrefix()][$name];
186  }
187 
194  public function hasErrors($name) {
195  if (isset($this->errors[$this->getPrefix()][$name])) {
196  return TRUE;
197  }
198  return FALSE;
199  }
200 
201 }
createRule($class, $arguments=array())
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
addRule($rule, $fieldName, $breakOnError=FALSE)