Exemplo n.º 1
0
 /**
  * 获取当前实例
  * @return XF_Db_Table_Validate
  */
 public static function getInstance()
 {
     if (self::$_instance === null) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Exemplo n.º 2
0
 /**
  * 进行数据验证
  * @param bool $validate 是否验证? 默认为true
  * @param bool $is_insert 当前验证的数据是否准备插入数据库?只有是插入数据库操作,所有的required强制生效
  * @throws XF_Db_Table_Exception
  * @return bool
  */
 protected function _validateAllData($validate = true, $is_insert = true)
 {
     if ($validate !== true) {
         return true;
     }
     if ($this->_field_validate_rule == null) {
         return true;
     }
     //验证
     $data = $this->toArray();
     if ($is_insert == false) {
         //只验证发生改变的字段
         $dbResultArray = $this->getDBResultArray();
         if ($dbResultArray != null && is_array($data)) {
             foreach ($dbResultArray as $key => $val) {
                 if (array_key_exists($key, $data) && $data[$key] === $val) {
                     unset($data[$key]);
                     //同时删除不需要验证的字段规则
                     $this->getFieldValidateRule()->removeFieldRule($key);
                 }
             }
         }
     }
     if (XF_Db_Table_Validate::getInstance()->validateData($data, $this->getFieldValidateRule()->toArray(), true, $this, $is_insert) === false) {
         throw new XF_Db_Table_Exception(XF_DataPool::getInstance()->get('TableFieldDataValidateError'));
     } else {
         $this->_set_var_validate = false;
         foreach ($data as $key => $val) {
             $this->{$key} = $val;
         }
         $this->_set_var_validate = true;
     }
     return true;
 }