[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/web_app/src/validation/rule/ -> lmbUniqueTableFieldRule.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/validation/src/rule/lmbSingleFieldRule.class.php');
  10  
  11  /**

  12   * class lmbUniqueTableFieldRule.

  13   *

  14   * @package web_app

  15   * @version $Id: lmbUniqueTableFieldRule.class.php 5945 2007-06-06 08:31:43Z pachanga $

  16   */
  17  class lmbUniqueTableFieldRule extends lmbSingleFieldRule
  18  {
  19    protected $table_name = '';
  20    protected $table_field = '';
  21    protected $error_message = '';
  22  
  23    function __construct($field_name, $table_name, $table_field = '', $error_message = '')
  24    {
  25      parent :: __construct($field_name);
  26  
  27      $this->table_name = $table_name;
  28      $this->table_field = $table_field ? $table_field : $field_name;
  29      $this->error_message = $error_message;
  30    }
  31  
  32    function check($value)
  33    {
  34      $conn = lmbToolkit :: instance()->getDefaultDbConnection();
  35  
  36      $sql = 'SELECT *
  37              FROM ' . $this->table_name . '
  38              WHERE  ' . $this->table_field . '=:value:';
  39  
  40      $stmt = $conn->newStatement($sql);
  41      $stmt->setVarChar('value', $value);
  42      $rs = $stmt->getRecordSet();
  43  
  44      if($rs->count() == 0)
  45        return;
  46  
  47      if($this->error_message)
  48        $this->error($this->error_message, array('Value' => $value));
  49      else
  50        $this->error(lmb_i18n('{Field} must have other value since {Value} already exists', 'web_app'),
  51                     array('Value' => $value));
  52    }
  53  }
  54  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7