![]() |
Form Validation - Modify Second Parameter - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Form Validation - Modify Second Parameter (/showthread.php?tid=48842) |
Form Validation - Modify Second Parameter - El Forum - 01-30-2012 [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) { 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. Form Validation - Modify Second Parameter - El Forum - 01-30-2012 [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 ....{ cheers ![]() Form Validation - Modify Second Parameter - El Forum - 01-30-2012 [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. Form Validation - Modify Second Parameter - El Forum - 01-30-2012 [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? Form Validation - Modify Second Parameter - El Forum - 01-30-2012 [eluser]Aken[/eluser] What do you mean you're unable to modify it? Can you give me a code example? Form Validation - Modify Second Parameter - El Forum - 01-30-2012 [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? Form Validation - Modify Second Parameter - El Forum - 01-30-2012 [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. Form Validation - Modify Second Parameter - El Forum - 01-30-2012 [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 Code: public function validate_clientAddress() { |