CodeIgniter: Validate form with multidimensional POST data

我只是一个虾纸丫 提交于 2019-12-05 03:05:46

The following controller code works for me on CI 2.0.2

public function test() {

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

        $this->form_validation->set_rules('test[test1][test2]', 'Test', 'required|valid_email');

        $this->form_validation->run();  

        echo validation_errors();

        echo form_open($this->uri->uri_string());
        echo form_input('test[test1][test2]', set_value('test[test1][test2]'));
        echo form_submit();
        echo form_close();

    }

You can use this to loop through the opt variable and set validation rules for each input.

if(!empty($opt))
    {
        foreach($opt as $id => $value)
        {
            $this->form_validation->set_rules('opt[' . $id . '][foo]', 'Foo', 'required');
            $this->form_validation->set_rules('opt[' . $id . '][bar]', 'Bar', 'required');
        }
    }

You should take a look at the callback functions for the validating class - this should help you accomplish what you need for validation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!