Removing characters before form_validation |
[eluser]roblarter[/eluser]
I am fairly new to CodeIgniter but have a lot of experience with PHP The problem I have is that I have an postcodes being submitted Code: <input name="addPostcode[]"> I want to remove spaces from the submitted field array before running the validation: Code: $this->form_validation->set_rules('addPostcode[]', 'Postcode', 'trim|alpha_numeric|xss_clean'); essentially the validation rule will cover anything other than spaces obviously as it only allows alpha_numeric but I want to ensure that it will "allow" spaces from the submitted user but just remove the space without giving an error to the user. Any help would be greatly appreciated
[eluser]Dam1an[/eluser]
You could do a callback which would replace non alphanumeric characters wityh underscores You'd have to have that before the alphanumeric check
[eluser]roblarter[/eluser]
I have tried a very crude way of doing this to test that earlier, but this simply breaks CodeIgniter, I may need some guidance on how it is possible to for example str_replace strings within CodeIgniter and its callback routine Code: $this->form_validation->set_rules('addPostcode[]', 'Postcode', 'callback_clean_postcode'); Code: function clean_postcode($str) { Cheers
[eluser]Dam1an[/eluser]
WHat do you mean when you say it breaks CI, what error do you get?
[eluser]roblarter[/eluser]
The breaking part was a small code error, now sorted that it simply loses the value of the string when I use that call back using the function I wrote
[eluser]Dam1an[/eluser]
I just tried it, and it works fine for me Here's my code Code: // Controller
[eluser]Evil Wizard[/eluser]
I thought the validation callback functions had to begin with an underscore to make the methods "private" in the controller, so the method name in the controller would be... Code: // Controller ty Dam1an, yes i did just copy n paste n slightly change yours lol
[eluser]Dam1an[/eluser]
@Evil: I though you needed the _ prefix, but check the user guide and it doesn't
[eluser]Evil Wizard[/eluser]
but if it doesn't have the "_" prefix it becomes accessible through the URL lol, actually remembering back, you don't need the "_", I added it to mine because it made the method accessable, Hmmmm I shall have to ponder this 1 ;o)
[eluser]jeffpeck[/eluser]
My understanding is that the callback function is only intended to return true or false. If you return $str modified that would always evaluate to true. You need to also edit the POST input. Right? |
Welcome Guest, Not a member yet? Register Sign In |