我在codeigniter中有一个包含多个字段的表单,但只有一个字段需要有效填写。
<label for="mail_everyone">Mass Email</label>
<label><input type="checkbox" name="mail_everyone" id="mail_everyone" value="1" <?php echo set_checkbox('mail_everyone', '1'); ?>/> Send to everyone</label>
<?php echo form_error('mail_everyone'); ?>
<label for="mail_class_list">Class Email</label>
<select name="mail_class_list" id="mail_class_list">
<option value="0">- Select -</option>
<?php foreach($filter_classes as $class) : ?>
<option value = "<?=$class['class_id']?>" <?php echo set_select('mail_class_list', $class['class_id'], ('class'==$mail_type&&$class['class_id']==$mail_type_id));?>><?=$class['class_code']?></option>
<?php endforeach; ?>
</select>
<?php echo form_error('mail_class_list'); ?>
<label for="mail_mailing_list">Mailing List Email</label>
<select name="mail_mailing_list" id="mail_mailing_list">
<option value="0"> - Select - </option>
<?php foreach($mailing_lists as $list){ ?>
<option value="<?=$list['mailer_list_id']?>" <?php echo set_select('mail_mailing_list', $list['mailer_list_id'], ('mailer.list'==$mail_type&&$list['mailer_list_id']==$mail_type_id));?>><?=$list['name']?></option>
<?php } ?>
</select>
<?php echo form_error('mail_mailing_list'); ?>
然后我有验证码:
$this->form_validation->_error_array = array(); // Multiform reset
$this->form_validation->_field_data = array(); // Multiform reset
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if ($this->form_validation->run() === false || isset($errors) && count($errors) > 0) {
$errors[] = validation_errors('<div>', '</div>');
$data['result'] = '<div class="error">' . implode('', $errors) . '</div>';
$this->session->set_flashdata('result', $data['result']);
} else {
// Check if a field has been filled in
if(
(!$this->input->post('mail_everyone') || 0 == set_value('mail_everyone')) &&
0 == set_value('mail_class_list') &&
0 == set_value('mail_performance_list') &&
0 == set_value('mail_mailing_list') &&
0 == set_value('mail_filter_list')
) {
//No filters have been set
$errors[] = validation_errors('<div>', '</div>');
$errors[] = '<div>No valid mail type has been selected</div>';
$data['result'] = '<div class="error">' . implode('', $errors) . '</div>';
$this->session->set_flashdata('result', $data['result']);
}
$data['result'] = '<div class="success">Inserted : <em>' . $this->settings->format_datetime() . '</em></div>';
$this->session->set_flashdata('result', $data['result']);
}
我需要让表单只需要填写一个字段。目前我检查验证,然后检查是否填写了任何字段,但此代码不起作用。这是我第一次深入研究codeigniter,文档对我没什么帮助。
答案 0 :(得分:0)
我发现字段以字符串形式返回,“0”而不是0,所以:
"0" == set_value('mail_filter_list')