Welcome Guest, Not a member yet? Register   Sign In
Form Validation - Modify Second Parameter
#1

[eluser]jamgood96[/eluser]
Is it possible to modify the second parameter for a custom callback?

Code:
$something = 14

Code:
$this->form_validation->set_rules('name', 'Name', 'callback_valid_name['.$something.']');

Code:
public function valid_address($value, $something) {

    $something = 20

}

Say I want to pass something to a custom callback. For example, I pass $something and it is equal to 14. After it gets passed and the callback function returns, $something is no equal to 20. Does that make sense?

I'm not having any luck with it thus far.
#2

[eluser]psychoder[/eluser]
never done that before... but got it working by using a protected or private variable( in a class),
assign the value to be used in a callback function to that variable and access or use that in your
callback function

Code:
class ....{

   protected $data_for_callback;

   ....

   method calling_form_validation()
   {
       $this->data_for_callback = value_to_be_used_in_call_back;
   }

   callback_method($param)
   {
       //access or use it now
       $this->data_for_callback ....
   }
}

cheers Wink
#3

[eluser]Aken[/eluser]
Passing a parameter value works fine in CI 2.1. What version of CodeIgniter are you using? Is your callback function set up appropriately?

Note that in your example you are calling the "valid_name" callback, but have shown code for a "valid_address" callback function.
#4

[eluser]jamgood96[/eluser]
[quote author="Aken" date="1327970457"]Passing a parameter value works fine in CI 2.1. What version of CodeIgniter are you using? Is your callback function set up appropriately?

Note that in your example you are calling the "valid_name" callback, but have shown code for a "valid_address" callback function.[/quote]

Sorry, that's a typo there. Should be callback_valid_address.

I'm running CI 2.1. I know passing a parameter works fine, but I'm unable to modify it. Is this due to variable scope?
#5

[eluser]Aken[/eluser]
What do you mean you're unable to modify it? Can you give me a code example?
#6

[eluser]jamgood96[/eluser]
[quote author="Aken" date="1327971042"]What do you mean you're unable to modify it? Can you give me a code example?[/quote]

I mean I can pass the variable to the callback function, but if I want to modify it, then access it after the callback function, I'm unable to.

Let's say the variable is equal to 0 prior to being passed to the callback. I send it to the callback, the callback then changes it to 1. If after the callback, I try to access the variable, it is still equal to 0.

Make sense?
#7

[eluser]Aken[/eluser]
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.
#8

[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;
}




Theme © iAndrew 2016 - Forum software by © MyBB