Welcome Guest, Not a member yet? Register   Sign In
Validation causes multi select to lose values
#1

[eluser]joelkinzel[/eluser]
First off, let me say that I tried to search here and Stack Overflow for an answer to this, but I can't seem to find anything that deals with this particular issue. When I did post on SO the only answer was to "restructure" my application (which isn't an option at this point).

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 (yes, I know, it isn't proper MVC, but I don't have a choice at this point, I need to finish the application in minimal time so changing that isn't an option at this point). The appropriate code is then returned based on what is passed to the model. The "form builder" model uses the form helper to build form elements based on data provided to it by the view (which in turn gets it from the controller, which in turn gets it from other models, which gets it from the database).

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.


Messages In This Thread
Validation causes multi select to lose values - by El Forum - 01-27-2013, 02:22 PM
Validation causes multi select to lose values - by El Forum - 01-27-2013, 03:50 PM
Validation causes multi select to lose values - by El Forum - 01-27-2013, 04:00 PM
Validation causes multi select to lose values - by El Forum - 01-27-2013, 07:16 PM



Theme © iAndrew 2016 - Forum software by © MyBB