我正在使用在config文件夹中创建的form_validation文件进行CodeIgniter表单验证
例如:
$config = array(
'signupBasic' => array(
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'required'
),
array(
'field' => 'firstName',
'label' => 'First Name',
'rules' => 'required'
),
array(
'field' => 'companyName',
'label' => 'Company Name',
'rules' => 'required'
),
array(
'field' => 'addMoreOfficer',
'label' => 'Add More Officers',
'rules' => 'callback_addmore_check'
),
)
);
问题是我有一些字段只有在选中复选框时才能验证我如何使用$ config数组方法实现此目的
答案 0 :(得分:1)
public function checkbox()
{
$data = array(
'name' => 'newsletter',
'id' => 'newsletter',
'value' => 'accept',
'checked' => FALSE,
'style' => 'margin:10px',
);
echo form_open('test/passingthrough');
echo form_checkbox($data);
echo form_submit('mysubmit', 'Submit CheckBox!');
}
public function passingthrough()
{
$this->form_validation->set_rules('mysubmit', '', 'required');
if ($this->form_validation->run() == FALSE) {
redirect('test/checkbox', 'refresh');
} else {
echo '<pre>', var_dump($this->input->post('newsletter'));
}
}
在您的环境中测试此代码。我认为您可以为以下代码设置$this->input->post('newsletter')
值的条件。