[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.