Welcome Guest, Not a member yet? Register   Sign In
Removing characters before form_validation
#1

[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
#2

[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
#3

[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) {
    $str = str_replace(" ","",$str);
    return $str;
}

Cheers
#4

[eluser]Dam1an[/eluser]
WHat do you mean when you say it breaks CI, what error do you get?
#5

[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
#6

[eluser]Dam1an[/eluser]
I just tried it, and it works fine for me
Here's my code

Code:
// Controller
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Test extends MY_Controller {
    function index() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('postcode[]', 'Postcode', 'callback_clean_postcode|trim|alpha_numeric|xss_clean');

        if($this->form_validation->run() == false) {
            $this->load->view('test/form');
        } else {
            print_r(set_value('postcode[]'));
        }
    }
    
    function clean_postcode($str) {
        return str_replace(' ', '_',$str);
    }
}
?>

// View
<form method="post">
    <input type="text" name="postcode[]" />
</form>
#7

[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
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Test extends MY_Controller {
    function index() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('postcode[]', 'Postcode', 'callback_clean_postcode|trim|alpha_numeric|xss_clean');

        if($this->form_validation->run() == false) {
            $this->load->view('test/form');
        } else {
            print_r(set_value('postcode[]'));
        }
    }
    
    function _clean_postcode($str) {
        return str_replace(' ', '',$str);
    }
}
?>

// View
<form method="post">
    <input type="text" name="postcode[]" />
</form>

ty Dam1an, yes i did just copy n paste n slightly change yours lol
#8

[eluser]Dam1an[/eluser]
@Evil: I though you needed the _ prefix, but check the user guide and it doesn't
#9

[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)
#10

[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?




Theme © iAndrew 2016 - Forum software by © MyBB