Welcome Guest, Not a member yet? Register   Sign In
form_validation - get field value in callback function
#1

[eluser]Dirk Einecke[/eluser]
Hi,

how can I get the value of a specific form field in a callback function of a form_validation rule? I asked because the content of the callback function depends on two form fields.

greetings
Dirk Einecke
#2

[eluser]saidai jagan[/eluser]
check <a href="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks">here</a>
#3

[eluser]Colin Williams[/eluser]
Code:
$this->input->post('other_field');
#4

[eluser]Dirk Einecke[/eluser]
Hi,

okay ... some code to clarify...

Code:
$this->form_validation->set_rules('username', 'User name', 'trim|required|min_length[5]|max_length[255]|callback_username_check');
$this->form_validation->set_rules('first_name', 'First name', 'trim|required|max_length[255]|xss_clean');

function username_check($str) {
  // at this point I need the value of "first_name"
  // after "trim" and "xss_clean" (defined in the rule of "first_name")
}

With $this->input->post('first_name'); I get only the original value of the "first_name" field and $str only contains the value of the "username" field.


Dirk
#5

[eluser]Colin Williams[/eluser]
All form_validation prepping is applied directly to the $_POST array. But, given the order in which your fields are defined, your username callback will fire before the validation class has a chance to process the first_name field.

In other words, you can't access the result of a process that has yet to happen.
#6

[eluser]Dirk Einecke[/eluser]
Hi,

[quote author="Colin Williams" date="1261166172"]All form_validation prepping is applied directly to the $_POST array.[/quote]

Thank's for this hint. That helps me.

[quote author="Colin Williams" date="1261166172"]But, given the order in which your fields are defined, your username callback will fire before the validation class has a chance to process the first_name field.[/quote]

Well .... okay. I will play a little bit with the order of the rules. I think I can solve my problem this way.

Tank's for your help!

Dirk




Theme © iAndrew 2016 - Forum software by © MyBB