Development

/branches/1.3/lib/plugins/sfPropelPlugin/lib/validator/sfValidatorPropelChoice.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfPropelPlugin/lib/validator/sfValidatorPropelChoice.class.php

Revision 28632, 4.0 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] fixed call to doCount (closes #7196)

  • 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  * sfValidatorPropelChoice validates that the value is one of the rows of a table.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfValidatorPropelChoice extends sfValidatorBase
20 {
21   /**
22    * Configures the current validator.
23    *
24    * Available options:
25    *
26    *  * model:      The model class (required)
27    *  * criteria:   A criteria to use when retrieving objects
28    *  * column:     The column name (null by default which means we use the primary key)
29    *                must be in field name format
30    *  * connection: The Propel connection to use (null by default)
31    *  * multiple:   true if the select tag must allow multiple selections
32    *  * min:        The minimum number of values that need to be selected (this option is only active if multiple is true)
33    *  * max:        The maximum number of values that need to be selected (this option is only active if multiple is true)
34    *
35    * @see sfValidatorBase
36    */
37   protected function configure($options = array(), $messages = array())
38   {
39     $this->addRequiredOption('model');
40     $this->addOption('criteria', null);
41     $this->addOption('column', null);
42     $this->addOption('connection', null);
43     $this->addOption('multiple', false);
44     $this->addOption('min');
45     $this->addOption('max');
46
47     $this->addMessage('min', 'At least %min% values must be selected (%count% values selected).');
48     $this->addMessage('max', 'At most %max% values must be selected (%count% values selected).');
49   }
50
51   /**
52    * @see sfValidatorBase
53    */
54   protected function doClean($value)
55   {
56     $criteria = null === $this->getOption('criteria') ? new Criteria() : clone $this->getOption('criteria');
57
58     if ($this->getOption('multiple'))
59     {
60       if (!is_array($value))
61       {
62         $value = array($value);
63       }
64
65       $count = count($value);
66
67       if ($this->hasOption('min') && $count < $this->getOption('min'))
68       {
69         throw new sfValidatorError($this, 'min', array('count' => $count, 'min' => $this->getOption('min')));
70       }
71
72       if ($this->hasOption('max') && $count > $this->getOption('max'))
73       {
74         throw new sfValidatorError($this, 'max', array('count' => $count, 'max' => $this->getOption('max')));
75       }
76
77       $criteria->addAnd($this->getColumn(), $value, Criteria::IN);
78
79       $dbcount = call_user_func(array(constant($this->getOption('model').'::PEER'), 'doCount'), $criteria, false, $this->getOption('connection'));
80
81       if ($dbcount != $count)
82       {
83         throw new sfValidatorError($this, 'invalid', array('value' => $value));
84       }
85     }
86     else
87     {
88       $criteria->addAnd($this->getColumn(), $value);
89
90       $dbcount = call_user_func(array(constant($this->getOption('model').'::PEER'), 'doCount'), $criteria, false, $this->getOption('connection'));
91
92       if (0 === $dbcount)
93       {
94         throw new sfValidatorError($this, 'invalid', array('value' => $value));
95       }
96     }
97
98     return $value;
99   }
100
101   /**
102    * Returns the column to use for comparison.
103    *
104    * The primary key is used by default.
105    *
106    * @return string The column name
107    */
108   protected function getColumn()
109   {
110     if ($this->getOption('column'))
111     {
112       $columnName = $this->getOption('column');
113       $from = BasePeer::TYPE_FIELDNAME;
114     }
115     else
116     {
117       $map = call_user_func(array(constant($this->getOption('model').'::PEER'), 'getTableMap'));
118       foreach ($map->getColumns() as $column)
119       {
120         if ($column->isPrimaryKey())
121         {
122           $columnName = $column->getPhpName();
123           break;
124         }
125       }
126       $from = BasePeer::TYPE_PHPNAME;
127     }
128
129     return call_user_func(array(constant($this->getOption('model').'::PEER'), 'translateFieldName'), $columnName, $from, BasePeer::TYPE_COLNAME);
130   }
131 }
132
Note: See TracBrowser for help on using the browser.