Manipulate input before using it with the CodeIgniter form validation class

Since you cannot do this: $this->form_validation->set_rules($VARIABLE, 'Some text', 'required');is it possible to do something similar to:

$variable = $this->input->post('some_input');
$variable = some_function_which_manipulates_the_input($variable);

$this->form_validation->set_rules($i_want_the_variable_here, '', '');

to control input before validation validation? Adding a custom callback seems a bit awkward to me, as one method can do several things (it is not necessary to focus on the X validation field).

+3
source share
3 answers

Since you cannot do this: $ this-> form_validation-> set_rules ($ VARIABLE, 'Some text', 'required');

Of course, you can do this as long as it $VARIABLEcontains the attribute of the namefield you want to check.

, $_POST set_rules() - . . :

http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules

$- > form_validation- > set_rules();

:

  • - , .
  • "" , message.Names.
  • .

, "prepping".

http://codeigniter.com/user_guide/libraries/form_validation.html#preppingdata

PHP, , , , htmlspecialchars, trim, MD5 ..

. prepping , , , .

, trim() - , . , , , php vanilla - , TRUE/FALSE - , . , , script, .

+2

$_POST , .

// Populate slug automatically
if (!$this->input->post('slug'))
{
    $_POST['slug'] = url_title($this->input->post('title'), '-', true);
}

$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('slug', 'Slug', 'trim|required|is_unique[categories.slug]');

if ($this->form_validation->run())
0

You can put input values ​​in an array before validation rules. I do not know what manipulations you want to do, but you can do such things.

 $dat = array(
           'fname' => filter_var($this->input->post('fname'), FILTER_SANITIZE_STRING),
           'lname' => filter_var($this->input->post('lname'), FILTER_SANITIZE_STRING),
           'email' => filter_var($this->input->post('email'), FILTER_SANITIZE_EMAIL),
           'phone' => $this->input->post('phone'),
           'relate' => filter_var($this->input->post('relate'), FILTER_SANITIZE_STRING),
            );
$this->form_validation->set_rules('lname', 'Last Name', 'required|trim|min_length[3]');

then on

 $this->db->update('contacts', $dat);

You can use most of the things in the array to manage it before setting the rules.

-1
source

All Articles