[eluser]jamgood96[/eluser]
[quote author="Aken" date="1327971366"]Yes, makes sense. You're correct then, it's a variable scope issue. The $param variable is created fresh each time the callback is run, based on the info passed to it in the rule. If you call "callback_valid_address[10]", $param = 10. "callback_valid_address[15]", $param = 15. Etc...
You'll want to use a class property if you want to hold on to the value of a property throughout the class. psychoder gave a good example.[/quote]
Hmm, bummer.
For a little insight, I was hoping to find a cleaner way to implement an address validator which would reach out and grab some addresses from Yahoo/Google. But since I'm really only limited to returning a TRUE/FALSE from the validator, and not easily returning an array of addresses, I'm not sure what else to do.
I currently having it working via two different javascript calls and a few functions in codeigniter. I suppose I should just leave well enough alone, but I know it can be done easier.
For grins 'n giggles, here's what I have so far...
Code:
// Submission of Add or Edit Client Form
$('#addOrEditClientForm').live('submit', function(e) {
// set hidden address field value
$('input[name=address]').val(
$('input[name=street]').val()+'#'+
$('input[name=city]').val()+'#'+
$('input[name=state]').val()+'#'+
$('input[name=zipcode]').val()
);
// set hidden telephone field value
$('input[name=telephone]').val(
$('input[name=telephoneA]').val()+'#'+
$('input[name=telephoneB]').val()+'#'+
$('input[name=telephoneC]').val()
);
// set hidden telephone2 field value
$('input[name=telephone2]').val(
$('input[name=telephone2A]').val()+'#'+
$('input[name=telephone2B]').val()+'#'+
$('input[name=telephone2C]').val()
);
validateAddOrEditClientForm();
e.preventDefault();
});
function validateAddOrEditClientForm() {
// lookup address first before validating rest of form
$('#addOrEditClientBox-loading').fadeIn(
function(){
// validate client address
validateClientAddress(
// callback function
function() {
// continue validating client information
validateClientInfo();
}
) // end validateClientAddress
}
); // end fadeIn
}
function validateClientAddress(callback) {
if ($('input[name=address]').val()=='###' || $('input[name=foundAddress]').val()=='true') {
callback();
} else {
$.post("validate_clientAddress","address="+$('input[name=address]').val()+"&csrf;_test_name="+$.cookie("csrf_cookie_name"),
function(data) {
if (data) {
if (data=="error") { // no matching addresses, set error and continue validating rest of form
$('input[name=foundAddress]').val('false');
callback(); // continue validating rest of form
}
else { // found possible matches
$('body').append(data); // append verifyClientAddressBox to body
// calculate position of box
var top = parseInt($('#addOrEditClientBox').css('top'))+
parseInt($('#addOrEditClientBox').css('height'))/2-
parseInt($('#verifyClientAddressBox').css('height'))/2;
$('#verifyClientAddressBox').css({'top':top}); // set new position
}
}
else callback(); // continue to validate client information if address is fine
}
); // end post
} // end else
}
/**
* Validate Client Info
*
* description
*/
function validateClientInfo() {
$.post("validate_addOrEditClientForm",$('#addOrEditClientForm').serialize(),
function(data){
var content = $(data).find('#addOrEditClientForm'); // retrieve just form portion of view
$('#addOrEditClientBox-loading').fadeIn(
function(){
$('#addOrEditClientForm').replaceWith(content);
}
);
$('#addOrEditClientBox-loading').fadeOut();
}
); // end $.post()
}
Code:
public function validate_clientAddress() {
$value = strtolower($this->input->post('address'));
$address = explode("#",$value);
$address = array_filter($address);
$addressURL = urlencode(implode(', ', $address));
// retrieves array of addresses from Yahoo
$addressData = file_get_contents('http://where.yahooapis.com/geocode?location='.$addressURL.'&flags=J');
$addressData = json_decode($addressData);
// determine if addresses found
if ($addressData && $addressData->ResultSet && $addressData->ResultSet->Error == '0' && $addressData->ResultSet->Found) {
for ($i=0; $i<$addressData->ResultSet->Found; $i++) {
$addresses[$i]->house = trim($addressData->ResultSet->Results[$i]->house);
$addresses[$i]->street = trim($addressData->ResultSet->Results[$i]->house.' '.$addressData->ResultSet->Results[$i]->street);
$addresses[$i]->city = trim($addressData->ResultSet->Results[$i]->city);
$addresses[$i]->state = trim($addressData->ResultSet->Results[$i]->statecode);
$addresses[$i]->zipcode = trim($addressData->ResultSet->Results[$i]->uzip);
if ($i==3) break; // limit to only 4 total results returned
}
} else {echo 'error'; return;} // if not found, give an error
// make sure full match found, not partial
if ($addresses[0]->house=="" || // don't allow missing street number from matches
$addresses[0]->street=="" ||
$addresses[0]->city=="" ||
$addresses[0]->state=="" ||
$addresses[0]->zipcode=="")
{echo 'error'; return;}; // if not found, give an error
if (count($address)==4) {
if (strtolower($addresses[0]->street)==$address[0] &&
strtolower($addresses[0]->city)==$address[1] &&
strtolower($addresses[0]->state)==$address[2] &&
strtolower($addresses[0]->zipcode)==$address[3])
{echo $addresses[0]->street.'#'.$addresses[0]->city.'#'.$addresses[0]->state.'#'.$addresses[0]->zipcode; return;}
}
$data['addresses'] = $addresses;
$this->load->view('partials/clients/verifyClientAddressBox',$data);
return;
}