Question

I have a form in codeigniter that has several fields, but only one needs to be filled in validly.

<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'); ?>

Then I have the validation code:

$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']);
}

I need to have the form only need one field to be filled in. At the moment I check the validation and then check if any fields are filled in, but this code doesn't work. This is my first delve into codeigniter and the documentation isn't helping me much.

Was it helpful?

Solution

I figured out the fields were coming back as strings and "0" worked instead of 0, so:

"0" == set_value('mail_filter_list')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top