CodeIgniter:使用多维POST数据验证表单 [英] CodeIgniter: Validate form with multidimensional POST data

查看:137
本文介绍了CodeIgniter:使用多维POST数据验证表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以框架是CodeIgniter 2.0.2。我有一个窗体,其中有与数据库中的行对应的字段组。字段的名称格式为:

  opt [0] [foo] 
opt [0] bar]
opt [1] [foo]
opt [1] [bar]
etc ...

索引(1,2,etc ...)不对应于数据库中的行ID,它只是一种分割字段组的方法。索引中可能有间隙,因为用户能够添加和删除任意数量的字段组。所有组都是相同的,也就是说,它们包含完全相同的具有相同第二级名称的字段集。



我想要能够使用CodeIgniter的验证库来验证形式和(p)根据需要重新填充。我发现大量的帖子(除了优秀的CI用户指南)预填充,我知道如何得到与一般的重新填充的工作。然而,这是我第一次不得不尝试它与索引字段名称如上。我尝试下面的,它不工作:

 数组(
'field'=> opt [] [foo]',
'label'=>'Foo',
'rules'=>'required'

我猜我只是希望太多,CodeIgniter不支持我需要它做。扩展现有的表单验证库一个选项,因此,如果任何人都处于相同的情况,并且可以提供一些非常受欢迎的提示。



UPDATE:



只是一点额外的信息,我也试过验证一个专门的索引字段(见下文),也没有工作...作为我理解它的多维验证应该在特定情况下工作:

  array(
'field'=> 0] [foo]',
'label'=>'Foo',
'rules'=>'required'


解决方案

以下控制器代码适用于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();

}


So the framework is CodeIgniter 2.0.2. I have a form that has groups of fields that correspond to rows in a database. The names of the fields are in the format:

opt[0][foo]
opt[0][bar]
opt[1][foo]
opt[1][bar]
etc...

The index (1,2,etc...) does not correspond to row IDs in the database, it is simply a way to split up the groups of fields. There may be gaps in the index as users are able to add and remove an arbitrary number of the field groups. All groups are identical, that is, they contain exactly the same set of fields with the same second level names.

I want to be able to use CodeIgniter's validation library to validate the form and (p)re-populate as necessary. I've found plenty of posts (in addition to the excellent CI user guide) on the pre-populating and I know how to get the working with the re-populating in general. However, this is the first time I've had to try it with the indexed field names as above. I've tried the below and it doesn't work:

array(
    'field' => 'opt[][foo]',
    'label' => 'Foo',
    'rules' => 'required'
)

I'm guessing I was just hoping for too much and CodeIgniter doesn't support what I need it to do. Extending the existing form validation library is an option so if anyone has been in the same situation and can provide some tips that would be very welcome.

UPDATE:

Just a little extra info, I've also tried validating a specifically indexed field (see below) and that also didn't work... As I understand it multidimensional validation should work in the specific case:

array(
    'field' => 'opt[0][foo]',
    'label' => 'Foo',
    'rules' => 'required'
)

解决方案

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();

    }

这篇关于CodeIgniter:使用多维POST数据验证表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆