CodeIgniter Forums
Field validation on maximum value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Field validation on maximum value (/showthread.php?tid=21726)



Field validation on maximum value - El Forum - 08-18-2009

[eluser]mdcode[/eluser]
I am trying to run validation on a form field where it checks to see if the number entered is higher than the number of records entered into the table in the database, but even though the tested value is higher, the error message does not display.

Code:
$this->form_validation->set_message('max_number', 'The value entered in the %s field must be a valid feedback item ID number.');

//some more code

/* set rule according to the selection of whether or not it is a repeat incident */
        $last_id = $this->db->count_all('feedback');
        
        if ( ($this->input->post('repeat') > 0) OR ($this->input->post('previous_id') > $last_id) )
        {
        $this->form_validation->set_rules('previous_id', 'Previous Complaint ID', 'required|min_length[1]|is_natural_no_zero|integer|max_number');
        }

Mucho appreciate any help.


Field validation on maximum value - El Forum - 08-18-2009

[eluser]verynewtothis[/eluser]
callback function are called like this:

Code:
$this->form_validation->set_rules('previous_id', 'Previous Complaint ID', 'required|min_length[1]|is_natural_no_zero|integer|callback_max_number');



Field validation on maximum value - El Forum - 08-18-2009

[eluser]mdcode[/eluser]
Thanks for your reply, but this hasn't happened before... Even if I place callback_ in front of the condition, validation seemingly fails to run on this field at all. I really don't understand what's going on with it since I have tested the clause with PHP.

The posted field value and variable is set.
$this->db->count_all() is returning a variable.
Actions based on these values return as expected, except the validation... :question:


Field validation on maximum value - El Forum - 08-18-2009

[eluser]verynewtothis[/eluser]
could you post the code for max_number callback function?


Field validation on maximum value - El Forum - 08-18-2009

[eluser]mdcode[/eluser]
Thanks again for the prompting... you saying what you have done, as me re-reading the documentation, I've realized what I have done - the previous one (which is working) I have added into the form validation files directly, where as here I needed to write an extra function (as per your prompting).

All is okay now.