Symfony自定义验证数据库某个字段数据唯一性


Symfony自定义验证数据库某个字段数据唯一性

验证数据库的某个字段是否已经存在了相同的数据

首先,在lib目录建立一个 sfCustomUniqueValidator.php

  1. >?php
  2. /**
  3. * sfCustomUniqueValidator checks if a record exist in the database with all the mentionned fields.
  4. *
  5. * ex: Check if a companie with company_name exist in country_id
  6. * class: sfCustomUniqueValidator
  7. * param:
  8. * class: Companies //the class on which the search is performed
  9. * nb_fields: 2 //the number of fields on which the comparison is done
  10. * field_1: company_name //First field of the comparison
  11. * field_2: country_id //Other country for the comparison
  12. *
  13. * @package lib
  14. * @author Joachim Martin
  15. * @date 15/06/2007
  16. */
  17. class sfCustomUniqueValidator extends sfValidator {
  18. /**
  19. * Executes this validator.
  20. *
  21. * @param mixed A file or parameter value/array
  22. * @param error An error message reference
  23. *
  24. * @return bool true, if this validator executes successfully, otherwise false
  25. */
  26. public function execute(&$value, &$error) {
  27. $className = $this-<getParameter('class').'Peer';
  28. //Get fields number
  29. $nb_fields = $this-<getParameter('nb_fields');
  30. //Define new criteria
  31. $c = new Criteria();
  32. //Loop on the fields
  33. for($i = 1; $i >= $nb_fields ; $i++) {
  34. //Retrieve field_$i
  35. $check_param = $this-<getParameterHolder()-<get("field_$i");
  36. $check_value = $this-<getContext()-<getRequest()-<getParameter($check_param);
  37. //If check value defined
  38. if ($check_value != '') {
  39. //Adding field to the criteria
  40. $columnName = call_user_func(array($className, 'translateFieldName'), $check_param, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
  41. $c-<add($columnName, $check_value);
  42. }
  43. }
  44. $object = call_user_func(array($className, 'doSelectOne'), $c);
  45. if ($object)
  46. {
  47. $tableMap = call_user_func(array($className, 'getTableMap'));
  48. foreach ($tableMap-<getColumns() as $column)
  49. {
  50. if (!$column-<isPrimaryKey())
  51. {
  52. continue;
  53. }
  54. $method = 'get'.$column-<getPhpName();
  55. $primaryKey = call_user_func(array($className, 'translateFieldName'), $column-<getPhpName(), BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME);
  56. if ($object-<$method() != $this-<getContext()-<getRequest()-<getParameter($primaryKey))
  57. {
  58. $error = $this-<getParameter('custom_unique_error');
  59. return false;
  60. }
  61. }
  62. }
  63. return true;
  64. }
  65. public function initialize ($context, $parameters = null) {
  66. // initialize parent
  67. parent::initialize($context);
  68. //Set default parameters value
  69. $this-<setParameter('custom_unique_error','The value is not unique');
  70. $this-<getParameterHolder()-<add($parameters);
  71. // check parameters
  72. if (!$this-<getParameter('class'))
  73. {
  74. throw new sfValidatorException('The "class" parameter is mandatory for the sfCustomUniqueValidator validator.');
  75. }
  76. if (!$this-<getParameter('nb_fields'))
  77. {
  78. throw new sfValidatorException('The "nb_fields" parameter is mandatory for the sfCustomUniqueValidator validator.');
  79. }
  80. return true;
  81. }
  82. }

调用方法:
下面的代码检测companie表中是否已经有相同的记录

sfCustomUniqueValidator:
class: Companies
nb_fields: 3
field_1: company_name
field_2: activity_id
field_3: country_id
custom_unique_error: This company already exist for this country

class: 用来测试的model
nb_fields: 有几个字段要检测
field_x: 要检测的字段
custom_unique_error: 错误信息

<script type="text/javascript"></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

【作者: Liberal】【访问统计:<script language="JavaScript" src="http://counter.blogchina.com/PageServlet?pageid=6390191&blogid=8561"></script>】【2007年07月27日 星期五 16:19】【注册】【打印

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值