Welcome Guest, Not a member yet? Register   Sign In
Validation Callback
#1

[eluser]jleequeen[/eluser]
Hello,

I'm having trouble getting a callback validation function to work. I've never used a callback before so I don't know if i'm doing it right. Below is the code. In the callback function I have set it up so that it will automatically trip the validation to false just to get it working. The callback is getting totally ignored. The validation on each of the other rules works fine, so I know it's got something to do with the callback function. Any ideas?

Code:
function distribution() {

     $this->load->library('validation');

     $rules['total_miles'] = 'trim|required|numeric';
     $rules['feet_replaced'] = 'trim|required|numeric';
     $rules['feet_added'] = 'trim|required|numeric';
     $rules['meters_replaced'] = 'trim|required|numeric';
     $rules['percent_check'] = 'callback_percent_check';

     $this->validation->set_rules($rules);

     if ($this->validation->run() == FALSE) {
          $this->load->view('distribution');
     } else {
          //save some stuff to the model
          redirect('somewhere');
     }
}

function percent_check() {
     $total = 20;
     if ($total == 100) {
         return TRUE;
     } else {
         $this->validation->set_message('percent_check', 'Estimated percentage of lines should equal 100%.');
         return FALSE;
     }
}
#2

[eluser]Colin Williams[/eluser]
The callback function needs to accept the value of the field as an argument:

Code:
function percent_check($total) {
     if ($total == 100) {
         return TRUE;
     } else {
         $this->validation->set_message('percent_check', 'Estimated percentage of lines should equal 100%.');
         return FALSE;
     }
}

Also, in your original code, you set $total to 20 then check if it's 100. It's always going to fail on that condition Smile
#3

[eluser]SneakyDave[/eluser]
Similar question about callback functions, I assume you can ONLY return a TRUE or FALSE with these functions, you can't return a value?
#4

[eluser]jleequeen[/eluser]
@Colin

I see, that would make sense. What I'm trying to do is validate that the sum of certain fields add up to 100, which obviously is not a field in the form. I guess i'll have to figure out another way to validate it. Also, I had set the $20 on purpose to make it fail while I was debugging.
#5

[eluser]jleequeen[/eluser]
Now that I think about it, how would I go about making a custom validation that is not directly connected to a certain field? Can you do that with the validation class?
#6

[eluser]Colin Williams[/eluser]
Quote:Now that I think about it, how would I go about making a custom validation that is not directly connected to a certain field? Can you do that with the validation class?

It's easy enough to get at the $_POST values with CI's Input class.

Code:
function percent_check($str) {
     if ($this->input->post('value_a') + $this->input->post('value_b') == 100) {
         return TRUE;
     } else {
         $this->validation->set_message('percent_check', 'Estimated percentage of lines should equal 100%.');
         return FALSE;
     }
}
#7

[eluser]jleequeen[/eluser]
@Colin

Ah, you make it seem to simple, duh! Thanks, I'm still getting use to this Validation class. You've been a huge help!
#8

[eluser]jleequeen[/eluser]
@Colin

When you use your example above, do you still have to pass $str to the percent_check function? Since you are directly doing the adding of two fields to see if they equal 100 in the function, it wouldn't be connected to a particular field. That's what I'm wondering if you can do or not.
#9

[eluser]Colin Williams[/eluser]
You can have a faux field set up with $this->validation->set_fields(), then do a callback rule on that field, but you DO NOT have to utilize the value it supplies to the callback function (there won't be one).
#10

[eluser]jleequeen[/eluser]
@Colin

I just updated my code and the callback is still not working. It's getting totally ignored. I did what you suggested by setting up a faux field using $this->validation->set_fields(), then did a callback rule on that field. I'm not sure what I'm doing wrong but it's pretty frustrating.

Here is my updated code:

Code:
function distribution() {

     $this->load->library('validation');

     $rules['total_miles'] = 'trim|required|numeric';
     $rules['feet_replaced'] = 'trim|required|numeric';
     $rules['feet_added'] = 'trim|required|numeric';
     $rules['meters_replaced'] = 'trim|required|numeric';
     $rules['percent_check'] = 'callback_percent_check';

     $this->validation->set_rules($rules);

     $fields['percent_check'] = 'faux';

     $this->validation->set_fields($fields);

     if ($this->validation->run() == FALSE) {
          $this->load->view('distribution');
     } else {
          //save some stuff to the model
          redirect('somewhere');
     }
}

function percent_check($str) {
     if ($this->input->post('value_a') + $this->input->post('value_b') == 100) {
         return TRUE;
     } else {
         $this->validation->set_message('percent_check', 'Estimated percentage of lines should equal 100%.');
         return FALSE;
     }
}

Any ideas? Sorry, I thought I had it working but alas not.




Theme © iAndrew 2016 - Forum software by © MyBB