本文整理汇总了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;
}