| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
class sfPropelUniqueValidator extends sfValidator |
|---|
| 37 |
{ |
|---|
| 38 |
public function execute(&$value, &$error) |
|---|
| 39 |
{ |
|---|
| 40 |
$className = $this->getParameter('class').'Peer'; |
|---|
| 41 |
$columnName = call_user_func(array($className, 'translateFieldName'), $this->getParameter('column'), BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME); |
|---|
| 42 |
|
|---|
| 43 |
$c = new Criteria(); |
|---|
| 44 |
$c->add($columnName, $value); |
|---|
| 45 |
$object = call_user_func(array($className, 'doSelectOne'), $c); |
|---|
| 46 |
|
|---|
| 47 |
if ($object) |
|---|
| 48 |
{ |
|---|
| 49 |
$tableMap = call_user_func(array($className, 'getTableMap')); |
|---|
| 50 |
foreach ($tableMap->getColumns() as $column) |
|---|
| 51 |
{ |
|---|
| 52 |
if (!$column->isPrimaryKey()) |
|---|
| 53 |
{ |
|---|
| 54 |
continue; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
$method = 'get'.$column->getPhpName(); |
|---|
| 58 |
$primaryKey = call_user_func(array($className, 'translateFieldName'), $column->getPhpName(), BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME); |
|---|
| 59 |
if ($object->$method() != $this->getContext()->getRequest()->getParameter($primaryKey)) |
|---|
| 60 |
{ |
|---|
| 61 |
$error = $this->getParameter('unique_error'); |
|---|
| 62 |
|
|---|
| 63 |
return false; |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
return true; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
* Initialize this validator. |
|---|
| 73 |
* |
|---|
| 74 |
* @param sfContext The current application context. |
|---|
| 75 |
* @param array An associative array of initialization parameters. |
|---|
| 76 |
* |
|---|
| 77 |
* @return bool true, if initialization completes successfully, otherwise false. |
|---|
| 78 |
*/ |
|---|
| 79 |
public function initialize($context, $parameters = null) |
|---|
| 80 |
{ |
|---|
| 81 |
|
|---|
| 82 |
parent::initialize($context); |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
$this->setParameter('unique_error', 'Uniqueness error'); |
|---|
| 86 |
|
|---|
| 87 |
$this->getParameterHolder()->add($parameters); |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
if (!$this->getParameter('class')) |
|---|
| 91 |
{ |
|---|
| 92 |
throw new sfValidatorException('The "class" parameter is mandatory for the sfPropelUniqueValidator validator.'); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
if (!$this->getParameter('column')) |
|---|
| 96 |
{ |
|---|
| 97 |
throw new sfValidatorException('The "column" parameter is mandatory for the sfPropelUniqueValidator validator.'); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
return true; |
|---|
| 101 |
} |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|