이것은 내 사용자 정의 유효성 검사 함수입니다. 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 answer과 the 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 answer은 My_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 라이브러리를 항상 사용하지 않기 때문에 자동으로로드하지 않기를 바란다. 내 방법 내에서 다른 문제가 있습니까?
을 고정 오류가 발생했지만 지금은 유효성 검사가 전혀 없습니다. – Sparky
함수의 맨 처음에'FALSE'를 반환하려고했는데 유효성 검사를했는지 확인하려고했습니다. ('$ str, 미국 '),'$ geocodes = $ CI-> gmap-> getGeoCode 실패하는거야? –
당신이 묻고있는 것이 확실하지 않지만, 테스트로서 조건부 바깥에서'return FALSE'를 움직였습니다. 유효성 검사 메시지가 작동합니다. 실패는 '$ 지오 코드'가 있어야 할 때조차도 '비어 있지 않다'는 것입니다. 나는'$ geocodes = $ CI-> gmap-> getGeoCode ("{$ str}, 미국)"와 함께 뭔가 잘못된 것 같아요. " – Sparky