开发者

Codeigniter validation help

I'm writing a system where users can generate/run queries on demand based on the values of 4 dropdown lists. The lists are dynamically generated based on a number of factors, but at this point, I'm having problems validating the input using codeigniter's built in validation classes. I think I have things out of order, and I've tried looking at the codeigniter site, but I think I'm tripping myself up.

in my view(/dashboard/dashboard_index.php), I have the following code block:

<?=form_open('dashboard/dashboard_add');?>
        <select ... name='selMetric'>
        <select ... name='selPeriod'>
        <select ... name='selSpan'>
        <select ... name='selTactic'>
        <input type="submit" name="submit_new_query" value="Add New Graph" class="minbutton" ></input>
<?=form_close();?>

Then in my controller, I have the following 2 methods:

function index() {

        $this->load->helper(array('form', 'url'));
        $this->load->library('validation');

        //population of $data

        $this->load->tile('dashboard/dashboard_index', $data);

    }

function dashboard开发者_如何学C_add()
    {
        $rules['selMetric'] = "callback_sel_check";
        $rules['selPeriod'] = "callback_sel_check";
        $rules['selSpan'] = "callback_sel_check";
        $rules['selTactic'] = "callback_sel_check";
        $this->validation->set_rules($rules);

        $fields['selMetric'] = "Metric";
        $fields['selPeriod'] = "Time Period";
        $fields['selSpan'] = "Time Span";
        $fields['selTactic'] = "Tactic";
        $this->validation->set_fields($fields);

        if ($this->validation->run() == false) {
            $this->index();
        } 
        else {
            //do stuff with validation information
        }
    }

Here's my issue. I can get the stuff to validate correctly, but for the number of errors I have, I get

Unable to access an error message corresponding to your field name.

as the error message for everything. I think my issue that I have the $rules and $fields stuff in the wrong place, but I've tried a few permutations and I just keep getting it wrong. I was hoping I could get some advice on the correct place to put things.


Are you setting error messages in your validation callback functions with a message key that matches your function name (sel_check in the example below)? That is, if your callback function is named sel_check, then $this->validation->set_message has to reference sel_check as well. Your callback functions should look something like this (forgive the slightly pseudo-code. I don't know what your validation functions actually do):

function sel_check($var) {
    if ($var == 'something') {
        $this->validation->set_message('sel_check', 'The %s field can not be the word "something"');
        return FALSE;
    } else {
        return TRUE;
    }
}

From the documentation:

The error message was set using the $this->validation->set_message function. Just remember that the message key (the first parameter) must match your function name.

Also, as noted at the top of the documentation page, the validation library you are using has been deprecated and use of the form_validation library is encouraged.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜