开发者

Validating single form field with CodeIgniter

Is there a quick way to validate a single form field with CodeIgniter to see whether or not that field matches a set of rules? There's the $this->form_validation->run();, but that will return either TRUE or FALSE for the whole form, and that's just not what I'm looking for. For example, if I only wanted to check if the email was valid, checking the whole form is not going to get me the result I'm looking for.

I looked through the documentation but couldn't find anything like $this->form_vali开发者_运维技巧dation->run(); that accepts one parameter and returns TRUE or FALSE if it's valid.


The form_validation class supports groups so you can define a group as email and run it like this $this->form_validation->run('email');


I was stuck in this problem, I solved it with form_error('field_name') , here the explanation:

1- First, you need to load form helper:

$this->load->helper('form');

2- you have to run the form validation:

$this->form_validation->run();

3- $this->form_validation->run(); will set the error messages, if it exists, to the helper function form_error('field_name'); and here you can check if it's false or will return a value.

Example:

    $this->load->library('form_validation');
    $this->load->helper('form');

    $this->form_validation->set_rules('field_one', 'First Field', 'numeric|required');
    $this->form_validation->set_rules('field_two', 'Second Field', 'required|min_length[5]|max_length[255]');

    $this->form_validation->run();
    $check['field_one'] = (form_error('field_one') ? form_error('field_one') : "Field one validated.. Success case");
    $check['field_two'] = (form_error('field_two') ? form_error('field_two') : "Field two validated.. Success case");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜