Welcome Guest, Not a member yet? Register   Sign In
extending CI_Validation class with MY_Validation class to add validation functions.
#1

[eluser]flyer[/eluser]
Hi all,

Really struggling to get to grips with a customised MY_Validation. The situation is I want a customised function similar to xss_clean which basically converts a text input string to a modified version (e.g. 'Bob' going to 'Hello Bob Hello'). I also want the validation function to be globally accessible i.e. not just a callback function within the Controller.

To this end I created a very simple custom MY_Validation.php vaguely following the patterns of the xss_clean function:

Code:
MY_Validation.php:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Validation extends CI_Validation {

    function MY_Validation()
    {
        parent::CI_Validation();
    }
    
    
    function customise_string( $str )
    {
        $_POST[$this->_current_field] = "Hello " + $str + " Hello";
    }
}
?>
The idea is if I can do this simple conversion, I can then move onto more complex modifications.

The code then used in the controller is:
Code:
function customiseStr()
    {
            $this->load->helper(array('form', 'url'));
            $this->load->library('validation');
            
            $rules['customStr'] = "required|customise_string";
            $this->validation->set_rules($rules);
            
            $fields['customStr'] = "Customised String";
            $this->validation->set_fields($fields);
            
            $this->validation->run();
            $this->load->view('content/customiseStr_view' );
    }
and the view is:
Code:
<html>
<head>
<title></title>
</head>
<body>

<?=$this->validation->error_string; ?>
<?=form_open    ('content/customiseStr' );?>
<?php
$customStr_value = ($this->validation->customStr == '') ? "String not yet entered" : $this->validation->customStr;
?>
<h5>Custom String:</h5>
&lt;?=form_input   ('customStr', $customStr_value)?&gt;
&lt;?=form_submit('submit', 'Customise')?&gt;
&lt;?=form_close()?&gt;

&lt;/body&gt;
&lt;/html&gt;

All looks very simple and as if it should work. Note the form simply submits to itself, then puts the modified string if available into the text input box. If I change customise_string to xss_clean in the $rules['customStr'] definition then works exactly how expected. i.e. on submit the string is xss_clean'ed then returned to the form and displayed in the text input box.

When the customise_string function is called in the MY_Validation class, I have been able to determine that before the $_POST variable is changed it equals the correct posted value, i.e. inputed string. But after is changed it equals null.

Is the problem that I need to treat the global $_POST in a special way when modifying it? Or is my code wrong in some other way?

Many thanks in advance.

Eddie
#2

[eluser]flyer[/eluser]
Hmmm, oooops, using the + sign instead of . to concatenate strings alas!!! sorry feel free to delete this post.




Theme © iAndrew 2016 - Forum software by © MyBB