Welcome Guest, Not a member yet? Register   Sign In
Multiselect Array Form Validation Problem
#1

[eluser]Unknown[/eluser]
Hello,

I'm trying to validate a multiselect array post with form validation class. But after validation, I have only the last item of the returning array...

I'm using Codeigniter 2.0.1...

construction of the form in viewer :
Code:
$skills = array('name' => 'skills[]', 'id' => 'skills', 'value' => ... 'options' => ...);
echo form_multiselect($skills['name'], $skills['options'], $skills['value']);

validation in controller :
Code:
$val = $this->form_validation;
$val->set_rules('skills[]', 'Skills', 'required');
$data['skills'] = $val->set_value('skills[]');

checking values :

Code:
print_r ($data['skills']) // returns :

6

// However, I'm waiting for an array as in $_POST.

print_r($_POST); // returns :

Array
(
    ...
    [skills] => Array
        (
            [0] => 4
            [1] => 6
        )
)

Where am I wrong ?
#2

[eluser]mariek[/eluser]
Hi,
I have the same problem. This code worked with CI 1.7.1, but it seems CI 2.0.1 changed that. Has anyone a solution ?
#3

[eluser]mariek[/eluser]
Fast solution :
instead of :
Code:
$permissions   = set_value('permissions[]');
do :
Code:
foreach($_POST['permissions'] as $tmp) {
    $permissions[] = set_value('permissions[]');
}
not clean but works
#4

[eluser]joelkinzel[/eluser]
Is there any better solution to this? My application is set up in a manner where the input is not being built in the controller, bur rather in a model, which is called by the view. The appropriate code is then returned based on what is passed to the model.

View:
Code:
foreach($category->questions as $question){
    $_args = array(
        'user'=>$user,
'question'=>$question
    );
    $fb = new Form_builder($_args);
    $attributes = array();
    if( ! $user->is_admin() && $question->admin_only){
$attributes['disabled'] = 'disabled';
    }
    if(form_error(underscore($question->question_text) . '[response]')){
$attributes['error'] = 'error';
    }
    echo $fb->build_input($attributes);  
}

Model (form_builder.php):
Code:
public function build_input(array $args = NULL){
    $_input_data = array(
         'name' => underscore($this->question->question_text).'[response]',
         'id'      => 'question-' . underscore($this->question->question_id),
         'class' => $this->question->required === '1' ? 'required' : ''
    );
          
    if ($args){
        if(in_array(strtolower('disabled'), $args)){
     $_input_data['disabled'] = 'disabled';
}
if(in_array(strtolower('readonly'), $args)){
     $_input_data['readonly'] = 'readonly';
}
if(in_array(strtolower('error'), $args)){
     $_input_data['class'] .= ' error';
}
    }

    if(strtolower($this->question->question_text) === 'authors'){
         return $this->_get_author_multiselect($_input_data);
     }
}
private function _get_author_multiselect($_input_data){
    $faculty = new Faculty();
    $response = $this->_get_user_response();
    $pre_selected;
    if(is_array($response)){
        foreach($response as $row){
     $pre_selected[$row->response_int] = $this->user->get_display_name($row->response_int, TRUE);
}
     } else if(is_object($response)) {
        $pre_selected[$response->response_int] = $this->user->get_display_name($response->response_int, TRUE);
    } else {
$pre_selected[$this->user->get_identity()] = $this->user->get_display_name($this->user->get_identity(), TRUE);
    }

    $additional_attributes = '';
    $_input_data['id'] = 'right-select';
    foreach($_input_data as $attribute=>$value){
$additional_attributes .= $attribute . '="' . $value . '" ';
    }
    $html = '<div class="fac-left"><span>Radiology Faculty</span>';

    $html .= form_multiselect('all-authors', set_value('all-authors', $faculty->get_all_for_multiselect()), array(), 'id="left-select"');

    $html .= '</div><div class="fac-actions"><button type="button" class="btn" id="add-fac">Add</button><button type="button" class="btn" id="remove-fac">Remove</button></div><div class="fac-right"><span>Selected Authors</span>';

    $html .= form_multiselect('authors[response][]', set_select('authors[response][]', $pre_selected, TRUE), array(), $additional_attributes);

    $html .= '</div>';

    return $html;
  }

When the page loads, the items are populated correctly, however when the user submits the form, if there are any errors, the selection is lost (or just one of the items is selected). Other items that are built using the same process, don't lose their values after failed validation, just multi selects.




Theme © iAndrew 2016 - Forum software by © MyBB