bc9a3947b073ed48cf87672fdc0286c53e7eeefd
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Validation / AlphabeticValidator.php
1 <?php
2 namespace TYPO3\CMS\Form\Validation;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008 Patrick Broens (patrick@patrickbroens.nl)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Alphabetic rule
28 *
29 * @author Patrick Broens <patrick@patrickbroens.nl>
30 * @package TYPO3
31 * @subpackage form
32 */
33 class AlphabeticValidator extends \TYPO3\CMS\Form\Validation\AbstractValidator {
34
35 /**
36 * Allow white space in the submitted value
37 *
38 * @var boolean
39 */
40 protected $allowWhiteSpace;
41
42 /**
43 * Alphabetic filter used for validation
44 *
45 * @var tx_form_Filter_Alphabetic
46 */
47 protected $filter;
48
49 /**
50 * Constructor
51 *
52 * @param array $arguments Typoscript configuration
53 * @return void
54 */
55 public function __construct($arguments = array()) {
56 $this->setAllowWhiteSpace($arguments['allowWhiteSpace']);
57 parent::__construct($arguments);
58 }
59
60 /**
61 * Returns TRUE if submitted value validates according to rule
62 *
63 * @return boolean
64 * @see tx_form_System_Validate_Interface::isValid()
65 */
66 public function isValid() {
67 if ($this->requestHandler->has($this->fieldName)) {
68 $value = $this->requestHandler->getByMethod($this->fieldName);
69 if ($this->filter === NULL) {
70 $className = 'TYPO3\\CMS\\Form\\Filter\\AlphabeticFilter';
71 $this->filter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className);
72 }
73 $this->filter->setAllowWhiteSpace($this->allowWhiteSpace);
74 if ($this->filter->filter($value) !== $value) {
75 return FALSE;
76 }
77 }
78 return TRUE;
79 }
80
81 /**
82 * Set TRUE if white space is allowed in submitted value
83 *
84 * @param boolean $allowWhiteSpace TRUE if white space allowed
85 * @return object Rule object
86 */
87 public function setAllowWhiteSpace($allowWhiteSpace) {
88 if ($allowWhiteSpace === NULL) {
89 $this->allowWhiteSpace = FALSE;
90 } else {
91 $this->allowWhiteSpace = (bool) $allowWhiteSpace;
92 }
93 return $this;
94 }
95
96 /**
97 * Get the local language label(s) for the message
98 * Overrides the abstract
99 *
100 * @return string The local language message label
101 * @see tx_form_System_Validate_Abstract::_getLocalLanguageLabel()
102 */
103 protected function getLocalLanguageLabel() {
104 $label = strtolower(get_class($this)) . '.message';
105 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label);
106 if ($this->allowWhiteSpace) {
107 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label . '2');
108 }
109 $message = implode(', ', $messages);
110 return $message;
111 }
112
113 }
114
115
116 ?>