Phalcon\Validation\Validator\ReCaptcha::validate PHP Method

validate() public method

public validate ( Phalcon\Validation $validation, string $attribute ) : boolean
$validation Phalcon\Validation
$attribute string
return boolean
    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;
    }
ReCaptcha