3

이것은 내 사용자 정의 유효성 검사 함수입니다. a Google Maps CodeIgniter library의 지오 코딩을 사용하여 위치가 있는지 확인합니다.CodeIgniter 폼 유효성 검사 라이브러리를 확장하려고합니다.

public function address_check($str) 
{ 
    $this->load->library('GMap'); 
    $this->gmap->GoogleMapAPI(); 

    // this method checks the cache and returns the cached response if available 
    $geocodes = $this->gmap->getGeoCode("{$str}, United States"); 

    $this->form_validation->set_message('address_check', 'The %s field contains an invalid address'); 

    if (empty($geocodes)) 
    { 
     return FALSE; 
    } 
    else 
    { 
     return TRUE; 
    } 
} 

위의 기능을 다음 규칙과 함께 내 컨트롤러에 배치하면 완벽하게 작동합니다.

$this->load->library('form_validation'); 
$this->form_validation->set_rules('location', 'Location', 'callback_address_check'); 

지금 난 그저 내 컨트롤러 밖으로 이동합니다. 그래서이 SO answerthe CI documentation에 따라 CodeIgniter 폼 유효성 검사 라이브러리를 확장하려고합니다. /codeigniter/application/libraries/MY_Form_validation.php : 내가 여기에 파일을 생성

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class MY_Form_validation extends CI_Form_validation { 

    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function address_check($str) 
    { 
     $this->load->library('GMap'); 
     $this->gmap->GoogleMapAPI(); 

     // this method checks the cache and returns the cached response if available 
     $geocodes = $this->gmap->getGeoCode("{$str}, United States"); 

     $this->form_validation->set_message('address_check', 'The %s field contains an invalid address'); 

     if (empty($geocodes)) 
     { 
      return FALSE; 
     } 
     else 
     { 
      return TRUE; 
     } 
    } 

} 

그리고 내 컨트롤러 내에서

, 나는 이런 식으로 규칙을 설정하고 ...

$this->load->library('form_validation'); 
$this->form_validation->set_rules('location', 'Location', 'address_check'); 

내가 찾은 자신을 해결 첫 번째 문제 that SO answerMy_Form_validation.php 인 파일 이름을 잘못 지정했기 때문에 아무런 변화가 없었습니다. MY_Form_validation.php

나는 내에서 라이브러리에 액세스 할 수 없습니다

$this->load->library('GMap'); 

:

Message: Undefined property: MY_Form_validation::$load

Filename: libraries/MY_Form_validation.php

Line Number: 12

이 라인 12 : 1,363,210

이제 함수가 호출되고 있는지, 새로운 문제는 다음과 같은 오류를 얻고 있다는 것입니다 도서관? 이 문제를 해결하는 적절한 방법은 무엇입니까? 나는 GMap 라이브러리를 항상 사용하지 않기 때문에 자동으로로드하지 않기를 바란다. 내 방법 내에서 다른 문제가 있습니까?

답변

5

사용이 : 다음

$CI =& get_instance(); 
$CI->load->library('GMap'); 

그리고이 좋아 사용

$CI->gmap->GoogleMapAPI(); 

를 양식 유효성 검사 CI 모델 또는 컨트롤러 클래스처럼되지 않기 때문에 당신은 그런 식으로해야 할, 그것은 단지 도서관의 .

+0

을 고정 오류가 발생했지만 지금은 유효성 검사가 전혀 없습니다. – Sparky

+0

함수의 맨 처음에'FALSE'를 반환하려고했는데 유효성 검사를했는지 확인하려고했습니다. ('$ str, 미국 '),'$ geocodes = $ CI-> gmap-> getGeoCode 실패하는거야? –

+0

당신이 묻고있는 것이 확실하지 않지만, 테스트로서 조건부 바깥에서'return FALSE'를 움직였습니다. 유효성 검사 메시지가 작동합니다. 실패는 '$ 지오 코드'가 있어야 할 때조차도 '비어 있지 않다'는 것입니다. 나는'$ geocodes = $ CI-> gmap-> getGeoCode ("{$ str}, 미국)"와 함께 뭔가 잘못된 것 같아요. " – Sparky

3

라이브러리 내부에서 먼저 CI 클래스를로드하여 다른 모델, 라이브러리, 구성, 도우미 등을 확장 할 수 있습니다. 예를 들어, 생성자에서 당신은 다음을 통해이 작업을 수행 할 수 있습니다

당신이 CI 클래스를로드 한 후
public function __construct() 
{ 
    parent::__construct(); 

    $this->ci =& get_instance(); 
} 

, 당신은 로딩을해야 할 수도 있습니다 다른 클래스를로드 할 수 있습니다.

예 :

$this->ci->load->library("session"); 
$this->ci->load->model("my_model"); 
$this->ci->load->helper("file"); 

또는 귀하의 경우 :

$this->ci->load->library("GMap"); 

그런 다음 클래스를 통해 비슷한 방식으로 클래스에서 함수를 호출 할 수 있습니다 :

$this->ci->gmap->GoogleMapAPI(); 
$this->ci->gmap->getGeoCode("{$str}, United States"); 
+0

다른 답변에서 논의 된 것과 같은 문제가 있습니다. 이렇게하면 모든 에러는 지워지지만'$ this-> ci-> gmap-> getGeoCode ("{$ str}, 미국");은 절대로 비어 있지 않습니다. 그리고 그것이 결코 비어 있지 않다면, 그것은 항상 검증을 통과합니다. 이 똑같은 기능이 내 컨트롤러에있을 때 제대로 작동합니다. – Sparky

+0

'var_dump ($ str)'과'var_dump ($ geocodes)'를 시도해 보았습니까? –

+0

네,'$ str'은 정확 합니다만,'$ geocodes'는 정크가 넘겨 지더라도 항상 lat/lon 위치를 리턴합니다. 나는 주소를 검증하는 더 좋은 방법을 찾아야 할 것이다. 이상하게도, 어제 내 컨트롤러에서 제대로 작동했다. 오늘날 내가 어디에 넣든 상관없이 작동하지 않습니다. – Sparky

관련 문제