Example #1
0
 public function validate(Validation $validator, $attribute)
 {
     $message = $this->getOption('message');
     $table = $validator->getEntity()->getSource();
     $di = $validator->getDI();
     if (strpos($attribute, '|') == 0) {
         $value = $validator->getValue($attribute);
         if (!$message) {
             $message = '您填写的' . $attribute . '已经被注册';
         }
         $stmt = $di['db']->prepare("SELECT COUNT(*) FROM {$table} WHERE {$attribute}=:value");
         $stmt->bindValue(':value', $value);
     } else {
         $attribute = explode('|', $attribute);
         $sql = "SELECT COUNT(*) FROM {$table} WHERE ";
         for ($i = 0; $i < count($attribute); $i++) {
             $a = $attribute[$i];
             if ($i != count($attribute) - 1) {
                 $sql .= "{$a}=? AND ";
             } else {
                 $sql .= "{$a}=?";
             }
         }
         $stmt = $di['db']->prepare($sql);
         for ($i = 0; $i < count($attribute); $i++) {
             $value = $validator->getValue($attribute[$i]);
             $stmt->bindValue($i + 1, $value);
         }
     }
     $stmt->execute();
     $result = $stmt->fetch(\PDO::FETCH_NUM);
     if ($result[0] == 1) {
         $validator->appendMessage(new Message($message, $attribute));
         return false;
     }
     return true;
 }