2017-01-24 9 views
2

Я пытаюсь добавить reCAPTCHA Google в свое веб-приложение Codeigniter.Codeigniter Ошибка проверки: «Не удалось получить сообщение об ошибке, соответствующее вашему имени поля Captcha. (Recaptcha)«

Но я продолжать работать в эту проблему: Unable to access an error message corresponding to your field name Captcha. (recaptcha)

Стараюсь подачи формы выдаст ошибку, $server_output['success'] должен быть FALSE, таким образом, выполняя $this->form_validation->set_message('recaptcha', 'Please redo the {field}.');, но не кажется Codeigniter, работает функция обратного вызова на всех. ..

Валидация работает на все остальное, как выходы ошибок (в дополнение к сообщению об ошибке в названии):

The Username field is required. The Password field is required. The Password Confirmation field is required. The Email field is required.

Я бег проверки через мою модель (после съезда держать контроллеры стройных и модель жира):

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->library('form_validation'); 
} 

public function register_validation() 
{ 
    /* 
    * INITIALIZE FORM VALIDATION RULES 
    */ 
    $this->form_validation->set_rules('username', 'Username', 
     array(
      // REMOVED FOR PRIVACY REASONS. 
     ) 
    ); 
    $this->form_validation->set_rules('password', 'Password', 
     array(
      // REMOVED FOR PRIVACY REASONS. 
      ) 
    ); 
    $this->form_validation->set_rules('password_confirmation', 'Password Confirmation', 
     array(
      // REMOVED FOR PRIVACY REASONS. 
      ) 
    ); 
    $this->form_validation->set_rules('email', 'Email', 
     array(
      // REMOVED FOR PRIVACY REASONS. 
     ) 
    ); 

    /* 
    * SET CUSTOM VERIFICATION RULES: 
    * 1. GOOGLE RECAPTCHA WIDGET 
    */ 
    $this->form_validation->set_rules('g-recaptcha-response', 'Captcha', 'callback_recaptcha'); 

    /* 
    * CHANGE ERROR MESSAGES FOR SOME RULES. 
    */ 
    $this->form_validation->set_message('is_unique', 'The {field} has already been taken!'); 

    /* 
    * RUN VALIDATION, 
    * IF VALIDATION IS NOT PASSED --> RETURN FALSE. 
    */ 
    if ($this->form_validation->run() === FALSE) 
     return FALSE; 
    /* 
    * ELSE WHEN EVERYTHING PASSES VALIDATION AND RECAPTCHA, 
    * TRY TO CREATE NEW USER. 
    */ 
    else 
    { 
     /* 
     * TRY TO CREATE NEW USER. 
     */ 
     if ($this->create_user()) 
     { 
      /* 
      * SET FLASHDATA TO ALERT USER THAT 
      * THE ACCOUNT HAS BEEN SUCCESSFULLY CREATED. 
      */ 
      $this->session->account_created = TRUE; 
      $this->session->mark_as_flash('account_created'); 
      return TRUE; 
     } 
     else 
     { 
      /* 
      * THROW ERROR AND ALERT USER VIA FLASHDATA. 
      */ 
      $this->session->account_created = FALSE; 
      $this->session->mark_as_flash('account_created'); 
      return FALSE; 
     } 
    } 
} 

Вот моя функция обратного вызова Recaptcha в том же классе/модели:

/* 
* RECAPTCHA FUNCTION 
*/ 
public function recaptcha($data = '') 
{ 
    /* 
    * INITIALIZE VARIABLES. 
    */ 
    $google_api_url = 'https://www.google.com/recaptcha/api/siteverify'; 
    $google_secret_key = 'REMOVED FOR PRIVACY REASONS'; 
    $ip = $this->input->ip_address(); 
    $response = $data; 

    /* 
    * INITIALIZE CURL. 
    */ 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $google_api_url); 
    curl_setopt($curl, CURLOPT_POST, 1); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, 
     http_build_query(array(
       'secret' => $google_secret_key, 
       'response' => $response, 
       'remoteip' => $ip, 
      ) 
     ) 
    ); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

    /* 
    * RUN CURL. 
    */ 
    $server_output = curl_exec($curl); 
    curl_close($curl); 
    $server_output = json_decode($server_output, true); 

    /* 
    * CHECK RECAPTCHA SUCCESS 
    */ 
    if ($server_output['success']) 
     return TRUE; 
    else 
    { 
     $this->form_validation->set_message('recaptcha', 'Please redo the {field}.'); 
     return FALSE; 
    } 
} 

Что я делаю неправильно? Это не работает, потому что я выполняю проверку в рамках модели? Застрял на этом в течение хороших 2-3 часов ... Надеюсь, код читаем.

Благодаря ~

ответ

1

“Unable to access an error message corresponding to your field name"

Я думаю, выше ошибки отображается, когда поле не имеет никакого значения и не требуемое правило:

Попробуйте установить правила как ниже

$this->form_validation->set_rules('g-recaptcha-response', 'Captcha', 'required|callback_recaptcha'); 

ИЛИ (Для последней версии CI)

$this->form_validation->set_rules(
     'g-recaptcha-response', /* Field */ 
     'Captcha',    /* Label */ 
     array(    /* Rules */ 
       'required', 
       array($this->your_model, 'recaptcha') 
      ) 
); 

ИЛИ

$this->form_validation->set_rules(
     'g-recaptcha-response', /* Field */ 
     'Captcha',    /* Label */ 
     array(    /* Rules */ 
       'required', 
       array('my_recaptcha', array($this->your_model, 'recaptcha')) 
      ), 
     array(    /* Error lists */ 
       'my_recaptcha' => 'Invalid Recaptcha' 
      ) 
); 

Смотреть больше here

+0

Еще такую ​​же ошибку ... Хм. – radiantMemory

+0

какой версия? –

+0

какая версия CodeIgniter? Последний. Версия 3.1.3 – radiantMemory

 Смежные вопросы

  • Нет связанных вопросов^_^