Welcome Guest, Not a member yet? Register   Sign In
validation help required
#1

[eluser]Computerzworld[/eluser]
Hello. I am having problem in validating my field values using CI validation class. I am having two comboboxes and I want that when user submits the form both the combobox have different value(Value can not be same). How can I do this using CI validation class? Please help me. Thanks in advance.
#2

[eluser]Computerzworld[/eluser]
got the solutionSmile
#3

[eluser]maesk[/eluser]
Hi,

how about posting your code? Otherwise this thread is kinda useless.
#4

[eluser]Computerzworld[/eluser]
Here is the callback function which i have used to compare two combobox's value....

Code:
function stationCheck($str)
    {
        if ($this->input->post("startStation")==$this->input->post("endStation"))
        {
            $this->validation->set_message('stationCheck', 'Start station and end station can not be same');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
#5

[eluser]wiredesignz[/eluser]
Code:
//this is a nicer solution
    function stationCheck()
    {
        $this->validation->set_message('stationCheck', 'Start station and end station can not be same');
        
        return (bool)($this->input->post("startStation") != $this->input->post("endStation"))
    }
#6

[eluser]maesk[/eluser]
Thank you, Computerzworld and wiredesignz for posting the code. I'm sure it'll prove useful for some people. I'm currently using the Validation class for the first time and although the user guide is great, as usual, I'm always glad to have a few examples to learn from.
#7

[eluser]Chillahan[/eluser]
I am curious - do people use the validation class for final values to input into database (or to otherwise process)? I just use input->post once validation->run() returns TRUE - since validation doesn't clean data, I assume it's safe to use input->post (since input class is the one that cleans data anyway).

Also, I would like to note a 'best practice' of mine regarding callback functions - I always keep them prefixed with _. Note that this means in your validation rules, when you assign the validation rule, it needs to have two underscores, not just one as in the User Guide examples (i.e., __data_check, not _data_check). If you do NOT do this, then you're basically exposing that callback function for public access! Imagine what you're opening up to a hacker anytime you start a new function in a controller file - do not forget this! For example, if you have a validation to check validity of some data, or even worse, to look it up in database, you're opening a wide open hole for a dictionary attack (or at best, a DOS attack on your mySQL daemon).
#8

[eluser]Colin Williams[/eluser]
I usually keep my callbacks private, too.

Also, wiredesignz, you don't even need the (bool) modifier:

Code:
//this is a nicer solution
    function stationCheck()
    {
        $this->validation->set_message('stationCheck', 'Start station and end station can not be same');
        
        return $this->input->post("startStation") != $this->input->post("endStation");
    }




Theme © iAndrew 2016 - Forum software by © MyBB