| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
* |
|---|
| 33 |
* @see sfValidatorBase |
|---|
| 34 |
*/ |
|---|
| 35 |
protected function configure($options = array(), $messages = array()) |
|---|
| 36 |
{ |
|---|
| 37 |
$this->addRequiredOption('model'); |
|---|
| 38 |
$this->addOption('criteria', null); |
|---|
| 39 |
$this->addOption('column', null); |
|---|
| 40 |
$this->addOption('connection', null); |
|---|
| 41 |
$this->addOption('multiple', false); |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
* @see sfValidatorBase |
|---|
| 46 |
*/ |
|---|
| 47 |
protected function doClean($value) |
|---|
| 48 |
{ |
|---|
| 49 |
$criteria = is_null($this->getOption('criteria')) ? new Criteria() : clone $this->getOption('criteria'); |
|---|
| 50 |
|
|---|
| 51 |
if ($this->getOption('multiple')) |
|---|
| 52 |
{ |
|---|
| 53 |
if (!is_array($value)) |
|---|
| 54 |
{ |
|---|
| 55 |
$value = array($value); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
$criteria->addAnd($this->getColumn(), $value, Criteria::IN); |
|---|
| 59 |
|
|---|
| 60 |
$objects = call_user_func(array(constant($this->getOption('model').'::PEER'), 'doSelect'), $criteria, $this->getOption('connection')); |
|---|
| 61 |
|
|---|
| 62 |
if (count($objects) != count($value)) |
|---|
| 63 |
{ |
|---|
| 64 |
throw new sfValidatorError($this, 'invalid', array('value' => $value)); |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
else |
|---|
| 68 |
{ |
|---|
| 69 |
$criteria->addAnd($this->getColumn(), $value); |
|---|
| 70 |
|
|---|
| 71 |
$object = call_user_func(array(constant($this->getOption('model').'::PEER'), 'doSelectOne'), $criteria, $this->getOption('connection')); |
|---|
| 72 |
|
|---|
| 73 |
if (is_null($object)) |
|---|
| 74 |
{ |
|---|
| 75 |
throw new sfValidatorError($this, 'invalid', array('value' => $value)); |
|---|
| 76 |
} |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
return $value; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
* Returns the column to use for comparison. |
|---|
| 84 |
* |
|---|
| 85 |
* The primary key is used by default. |
|---|
| 86 |
* |
|---|
| 87 |
* @return string The column name |
|---|
| 88 |
*/ |
|---|
| 89 |
protected function getColumn() |
|---|
| 90 |
{ |
|---|
| 91 |
if ($this->getOption('column')) |
|---|
| 92 |
{ |
|---|
| 93 |
$columnName = $this->getOption('column'); |
|---|
| 94 |
$from = BasePeer::TYPE_FIELDNAME; |
|---|
| 95 |
} |
|---|
| 96 |
else |
|---|
| 97 |
{ |
|---|
| 98 |
$map = call_user_func(array(constant($this->getOption('model').'::PEER'), 'getTableMap')); |
|---|
| 99 |
foreach ($map->getColumns() as $column) |
|---|
| 100 |
{ |
|---|
| 101 |
if ($column->isPrimaryKey()) |
|---|
| 102 |
{ |
|---|
| 103 |
$columnName = $column->getPhpName(); |
|---|
| 104 |
break; |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
$from = BasePeer::TYPE_PHPNAME; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
return call_user_func(array(constant($this->getOption('model').'::PEER'), 'translateFieldName'), $columnName, $from, BasePeer::TYPE_COLNAME); |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|