Development

/branches/1.1/lib/plugins/sfCompat10Plugin/lib/validator/sfStringValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfCompat10Plugin/lib/validator/sfStringValidator.class.php

Revision 10270, 4.6 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: added deprecated warnings to all sfCompatPlugin classes (closes #3882)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfStringValidator allows you to apply string-related constraints to a
14  * parameter.
15  *
16  * <b>Optional parameters:</b>
17  *
18  * # <b>insensitive</b>  - [false]              - Whether or not the value check
19  *                                                against the array of values is
20  *                                                case-insensitive. <b>Note:</b>
21  *                                                When using this option, values
22  *                                                in the values array must be
23  *                                                entered in lower-case.
24  * # <b>max</b>          - [none]               - Maximum string length.
25  * # <b>max_error</b>    - [Input is too long]  - An error message to use when
26  *                                                input is too long.
27  * # <b>min</b>          - [none]               - Minimum string length.
28  * # <b>min_error</b>    - [Input is too short] - An error message to use when
29  *                                                input is too short.
30  * # <b>values</b>       - [none]               - An array of values the input
31  *                                                is allowed to match.
32  * # <b>values_error</b> - [Invalid selection]  - An error message to use when
33  *                                                input does not match a value
34  *                                                listed in the values array.
35  *
36  * WARNING: This class is deprecated and will be removed in symfony 1.2.
37  *
38  * @package    symfony
39  * @subpackage validator
40  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
41  * @author     Sean Kerr <sean@code-box.org>
42  * @version    SVN: $Id$
43  * @deprecated Deprecated since symfony 1.1
44  */
45 class sfStringValidator extends sfValidator
46 {
47   /**
48    * Executes this validator.
49    *
50    * @param mixed A parameter value
51    * @param error An error message reference
52    *
53    * @return bool true, if this validator executes successfully, otherwise false
54    */
55   public function execute(&$value, &$error)
56   {
57     $decodedValue = sfToolkit::isUTF8($value) && function_exists('utf8_decode') ? utf8_decode($value) : $value;
58
59     $min = $this->getParameterHolder()->get('min');
60     if ($min !== null && strlen(trim($decodedValue)) < $min)
61     {
62       // too short
63       $error = $this->getParameterHolder()->get('min_error');
64
65       return false;
66     }
67
68     $max = $this->getParameterHolder()->get('max');
69     if ($max !== null && strlen(trim($decodedValue)) > $max)
70     {
71       // too long
72       $error = $this->getParameterHolder()->get('max_error');
73
74       return false;
75     }
76
77     $values = $this->getParameterHolder()->get('values');
78     if ($values !== null)
79     {
80       if ($this->getParameterHolder()->get('insensitive'))
81       {
82         $value = strtolower($value);
83         $found = false;
84         foreach ($values as $avalue)
85         {
86           if ($value == strtolower($avalue))
87           {
88             $found = true;
89             break;
90           }
91         }
92         if (!$found)
93         {
94           // can't find a match
95           $error = $this->getParameterHolder()->get('values_error');
96
97           return false;
98         }
99       }
100       else
101       {
102         if (!in_array($value, (array) $values))
103         {
104           // can't find a match
105           $error = $this->getParameterHolder()->get('values_error');
106
107           return false;
108         }
109       }
110     }
111
112     return true;
113   }
114
115   /**
116    * Initializes this validator.
117    *
118    * @param sfContext The current application context
119    * @param array   An associative array of initialization parameters
120    *
121    * @return bool true, if initialization completes successfully, otherwise false
122    */
123   public function initialize($context, $parameters = null)
124   {
125     // initialize parent
126     parent::initialize($context);
127
128     // set defaults
129     $this->getParameterHolder()->set('insensitive'false);
130     $this->getParameterHolder()->set('max',          null);
131     $this->getParameterHolder()->set('max_error',    'Input is too long');
132     $this->getParameterHolder()->set('min',          null);
133     $this->getParameterHolder()->set('min_error',    'Input is too short');
134     $this->getParameterHolder()->set('values',       null);
135     $this->getParameterHolder()->set('values_error', 'Invalid selection');
136
137     $this->getParameterHolder()->add($parameters);
138
139     return true;
140   }
141 }
142
Note: See TracBrowser for help on using the browser.