开发者

Codeigniter - form validation callback order

Hopefully a simple one. I have the following form validation rules:

    $this->form_validation->set_rules('timeStart', 'Day Start Time', 'trim|required|xss_clean|callback_validtime');
    $this->form_validation->set_rules('timeEnd', 'Day End Time', 'trim|required|xss_clean|callback_validtime|callback_validtimeorder');
    $this->form_validation->set_rules('interval', 'Interval', 'trim|required|xss_clean|is_natural|callback_valid_interval');

The problem I have is that callback_valid_interval requires that $this->input->post('timeStart') and $this->input->post('timeEnd') are set. Currently my callback_valid_interval function errors if the user has not completed the timeStart or timeEnd.

Is there any way I can set a dependency between the rules i.e. dont run the callback_valid_interval if the other rules have not passed?

Alternatively I can put in a manual check for these fields but was wonderin开发者_Python百科g if there is a better way to do this?

Best regards, Ben.


In Codeigniter rules are ran left to right, as soon as one fails, it stops checking and marks the field as 'not passed validation' and set the message as the first failed rule.

You have all 3 fields as required so at a minimum you will have to have something in all 3 fields. Each field is checked independently, so no you cannot (with the built in form_validation Class) create logical checks that are more complex than each field having independent rules.


Unfortunately, except for the matches[] rule, nothing in Codeigniter's form validation class allows you to do this...

Since CI_Form_validation::set_rules does not return anything, you cannot use it in your if clause. I suggest you simply check if your values are set.

They will not be validated when your third rule callback is run, so they could be anything : do some checking in callback_valid_interval.

Something like this :

$this->form_validation->set_rules('timeStart', 'Day Start Time', 'trim|required|xss_clean|callback_validtime');
$this->form_validation->set_rules('timeEnd', 'Day End Time', 'trim|required|xss_clean|callback_validtime|callback_validtimeorder');
if ($this->input->post('timeStart') && $this->input->post('timeEnd')){
    $this->form_validation->set_rules('interval', 'Interval', 'trim|required|xss_clean|is_natural|callback_valid_interval');
}


Using the if(true AND true) method does what you want but I want to make sure you know what it does. set_rules doesn't evaluate rules, only the run function does. What jfoucher's code does is get the post values and if they aren't both set then it will create another rule. But this doesn't check trim or any other rule (other than required). However that method does work.

The only alternative I can think of is to rewrite you callbacks, or make your own rule by extending the form_validation class. I did the extension for my applications and it gives me a more universal way of validation, but it requires you to dig into CodeIgniter's core.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜