Development

/branches/1.4/lib/plugins/sfDoctrinePlugin/lib/validator/sfValidatorDoctrineChoice.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/plugins/sfDoctrinePlugin/lib/validator/sfValidatorDoctrineChoice.class.php

Revision 27736, 3.7 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] fixed sfValidatorDoctrineChoice in cloned forms (embedForEach) doesn't function correctly (closes #8198)

  • 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  * (c) Jonathan H. Wage <jonwage@gmail.com>
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  * sfValidatorDoctrineChoice validates that the value is one of the rows of a table.
14  *
15  * @package    symfony
16  * @subpackage doctrine
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Jonathan H. Wage <jonwage@gmail.com>
19  * @version    SVN: $Id$
20  */
21 class sfValidatorDoctrineChoice extends sfValidatorBase
22 {
23   /**
24    * Configures the current validator.
25    *
26    * Available options:
27    *
28    *  * model:      The model class (required)
29    *  * query:      A query to use when retrieving objects
30    *  * column:     The column name (null by default which means we use the primary key)
31    *                must be in field name format
32    *  * multiple:   true if the select tag must allow multiple selections
33    *  * min:        The minimum number of values that need to be selected (this option is only active if multiple is true)
34    *  * max:        The maximum number of values that need to be selected (this option is only active if multiple is true)
35    *
36    * @see sfValidatorBase
37    */
38   protected function configure($options = array(), $messages = array())
39   {
40     $this->addRequiredOption('model');
41     $this->addOption('query', null);
42     $this->addOption('column', 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     if ($query = $this->getOption('query'))
57     {
58       $query = clone $query;
59     }
60     else
61     {
62       $query = Doctrine_Core::getTable($this->getOption('model'))->createQuery();
63     }
64
65     if ($this->getOption('multiple'))
66     {
67       if (!is_array($value))
68       {
69         $value = array($value);
70       }
71
72       if (isset($value[0]) && !$value[0])
73       {
74         unset($value[0]);
75       }
76
77       $count = count($value);
78
79       if ($this->hasOption('min') && $count < $this->getOption('min'))
80       {
81         throw new sfValidatorError($this, 'min', array('count' => $count, 'min' => $this->getOption('min')));
82       }
83
84       if ($this->hasOption('max') && $count > $this->getOption('max'))
85       {
86         throw new sfValidatorError($this, 'max', array('count' => $count, 'max' => $this->getOption('max')));
87       }
88
89       $query->andWhereIn(sprintf('%s.%s', $query->getRootAlias(), $this->getColumn()), $value);
90
91       if ($query->count() != count($value))
92       {
93         throw new sfValidatorError($this, 'invalid', array('value' => $value));
94       }
95     }
96     else
97     {
98       $query->andWhere(sprintf('%s.%s = ?', $query->getRootAlias(), $this->getColumn()), $value);
99
100       if (!$query->count())
101       {
102         throw new sfValidatorError($this, 'invalid', array('value' => $value));
103       }
104     }
105
106     return $value;
107   }
108
109   /**
110    * Returns the column to use for comparison.
111    *
112    * The primary key is used by default.
113    *
114    * @return string The column name
115    */
116   protected function getColumn()
117   {
118     $table = Doctrine_Core::getTable($this->getOption('model'));
119     if ($this->getOption('column'))
120     {
121       $columnName = $this->getOption('column');
122     }
123     else
124     {
125       $identifier = (array) $table->getIdentifier();
126       $columnName = current($identifier);
127     }
128
129     return $table->getColumnName($columnName);
130   }
131 }
132
Note: See TracBrowser for help on using the browser.