| 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 |
* |
|---|
| 32 |
* @see sfValidatorBase |
|---|
| 33 |
*/ |
|---|
| 34 |
protected function configure($options = array(), $messages = array()) |
|---|
| 35 |
{ |
|---|
| 36 |
$this->addRequiredOption('model'); |
|---|
| 37 |
$this->addOption('criteria', null); |
|---|
| 38 |
$this->addOption('column', null); |
|---|
| 39 |
$this->addOption('connection', null); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* @see sfValidatorBase |
|---|
| 44 |
*/ |
|---|
| 45 |
protected function doClean($value) |
|---|
| 46 |
{ |
|---|
| 47 |
$criteria = is_null($this->getOption('criteria')) ? new Criteria() : clone $this->getOption('criteria'); |
|---|
| 48 |
$criteria->addAnd($this->getColumn(), $value); |
|---|
| 49 |
|
|---|
| 50 |
$object = call_user_func(array($this->getOption('model').'Peer', 'doSelectOne'), $criteria, $this->getOption('connection')); |
|---|
| 51 |
|
|---|
| 52 |
if (is_null($object)) |
|---|
| 53 |
{ |
|---|
| 54 |
throw new sfValidatorError($this, 'invalid', array('value' => $value)); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
return $value; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
* Returns the column to use for comparison. |
|---|
| 62 |
* |
|---|
| 63 |
* The primary key is used by default. |
|---|
| 64 |
* |
|---|
| 65 |
* @return string The column name |
|---|
| 66 |
*/ |
|---|
| 67 |
protected function getColumn() |
|---|
| 68 |
{ |
|---|
| 69 |
if ($this->getOption('column')) |
|---|
| 70 |
{ |
|---|
| 71 |
$columnName = $this->getOption('column'); |
|---|
| 72 |
} |
|---|
| 73 |
else |
|---|
| 74 |
{ |
|---|
| 75 |
$map = call_user_func(array($this->getOption('model').'Peer', 'getTableMap')); |
|---|
| 76 |
foreach ($map->getColumns() as $column) |
|---|
| 77 |
{ |
|---|
| 78 |
if ($column->isPrimaryKey()) |
|---|
| 79 |
{ |
|---|
| 80 |
$columnName = strtolower($column->getColumnName()); |
|---|
| 81 |
break; |
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
return call_user_func(array($this->getOption('model').'Peer', 'translateFieldName'), $columnName, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME); |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|