Development

/branches/1.2/lib/validator/sfValidatorAnd.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/validator/sfValidatorAnd.class.php

Revision 13127, 3.8 kB (checked in by nicolas, 4 years ago)

[1.2] forthport of r13126: sfValidatorAnd and sfValidatorOr validators are now required by default to ensure a standard behavior - fixes and closes #4877

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfValidatorAnd validates an input value if all validators passes.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfValidatorAnd extends sfValidatorBase
20 {
21   protected
22     $validators = array();
23
24   /**
25    * Constructor.
26    *
27    * The first argument can be:
28    *
29    *  * null
30    *  * a sfValidatorBase instance
31    *  * an array of sfValidatorBase instances
32    *
33    * @param mixed $validators Initial validators
34    * @param array $options    An array of options
35    * @param array $messages   An array of error messages
36    *
37    * @see sfValidatorBase
38    */
39   public function __construct($validators = null, $options = array(), $messages = array())
40   {
41     if ($validators instanceof sfValidatorBase)
42     {
43       $this->addValidator($validators);
44     }
45     else if (is_array($validators))
46     {
47       foreach ($validators as $validator)
48       {
49         $this->addValidator($validator);
50       }
51     }
52     else if (!is_null($validators))
53     {
54       throw new InvalidArgumentException('sfValidatorAnd constructor takes a sfValidatorBase object, or a sfValidatorBase array.');
55     }
56     
57     parent::__construct($options, $messages);
58   }
59
60   /**
61    * Configures the current validator.
62    *
63    * Available options:
64    *
65    *  * halt_on_error: Whether to halt on the first error or not (false by default)
66    *
67    * @param array $options   An array of options
68    * @param array $messages  An array of error messages
69    *
70    * @see sfValidatorBase
71    */
72   protected function configure($options = array(), $messages = array())
73   {
74     $this->addOption('halt_on_error', false);
75
76     $this->setMessage('invalid', null);
77   }
78
79   /**
80    * Adds a validator.
81    *
82    * @param sfValidatorBase $validator  A sfValidatorBase instance
83    */
84   public function addValidator(sfValidatorBase $validator)
85   {
86     $this->validators[] = $validator;
87   }
88
89   /**
90    * Returns an array of the validators.
91    *
92    * @return array An array of sfValidatorBase instances
93    */
94   public function getValidators()
95   {
96     return $this->validators;
97   }
98
99   /**
100    * @see sfValidatorBase
101    */
102   protected function doClean($value)
103   {
104     $clean = $value;
105     $errors = array();
106     foreach ($this->validators as $validator)
107     {
108       try
109       {
110         $clean = $validator->clean($clean);
111       }
112       catch (sfValidatorError $e)
113       {
114         $errors[] = $e;
115
116         if ($this->getOption('halt_on_error'))
117         {
118           break;
119         }
120       }
121     }
122
123     if (count($errors))
124     {
125       if ($this->getMessage('invalid'))
126       {
127         throw new sfValidatorError($this, 'invalid', array('value' => $value));
128       }
129
130       throw new sfValidatorErrorSchema($this, $errors);
131     }
132
133     return $clean;
134   }
135
136   /**
137    * @see sfValidatorBase
138    */
139   public function asString($indent = 0)
140   {
141     $validators = '';
142     for ($i = 0, $max = count($this->validators); $i < $max; $i++)
143     {
144       $validators .= "\n".$this->validators[$i]->asString($indent + 2)."\n";
145
146       if ($i < $max - 1)
147       {
148         $validators .= str_repeat(' ', $indent + 2).'and';
149       }
150
151       if ($i == $max - 2)
152       {
153         $options = $this->getOptionsWithoutDefaults();
154         $messages = $this->getMessagesWithoutDefaults();
155
156         if ($options || $messages)
157         {
158           $validators .= sprintf('(%s%s)',
159             $options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
160             $messages ? ', '.sfYamlInline::dump($messages) : ''
161           );
162         }
163       }
164     }
165
166     return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent));
167   }
168 }
169
Note: See TracBrowser for help on using the browser.