本文整理匯總了PHP中Phalcon\Validation::getDI方法的典型用法代碼示例。如果您正苦於以下問題:PHP Validation::getDI方法的具體用法?PHP Validation::getDI怎麽用?PHP Validation::getDI使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Phalcon\Validation
的用法示例。
在下文中一共展示了Validation::getDI方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: validate
/**
* {@inheritdoc}
*
* @param Validation $validation
* @param string $attribute
*
* @return bool
*/
public function validate(Validation $validation, $attribute)
{
$secret = $this->getOption('secret');
$value = $validation->getValue($attribute);
$request = $validation->getDI()->get('request');
$remoteIp = $request->getClientAddress(false);
if (!empty($value)) {
$curl = curl_init(self::RECAPTCHA_URL);
curl_setopt_array($curl, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => ['secret' => $secret, 'response' => $value, 'remoteip' => $remoteIp]]);
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
}
if (empty($response['success'])) {
$label = $this->getOption('label');
if (empty($label)) {
$label = $validation->getLabel($attribute);
}
$message = $this->getOption('message');
$replacePairs = [':field', $label];
if (empty($message) && !empty($response['error-codes'])) {
$message = $this->messages[$response['error-codes']];
}
if (empty($message)) {
$message = $validation->getDefaultMessage('ReCaptcha');
}
$validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, 'ReCaptcha'));
return false;
}
return true;
}
示例2: validate
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;
}