The correct way to work with data in elements array

The correct way to work with data in elements array

by Viktor Merinov -
Number of replies: 2

$repeatarray = array();
$repeatarray[] = $form->createElement('text','courseid', 'Выберите курс');
$repeatarray[] = $form->createElement('select','testid', 'Выберите тест', get_tests_list($this->course_id));
$repeateloptions = array();
$repeatno = 0;
//if edit mode
if ($this->_data && !empty((array)$this->_data)){
$repeatno = count((array)$this->_customdata);
} else {
$repeatno = 1;
}
$this->repeat_elements($repeatarray, $repeatno, $repeateloptions, 'option_repeats', 'option_add_fields', 1, 'Добавить тест', true);
for($i=0;$i<$repeatno;$i++){
$this->init_course_id($i);
$form->addRule('courseid['.$i.']', 'Ведите название курса', 'required', null, 'client');
$form->addRule('testid['.$i.']', 'Выберите тест', 'required', null, 'client');
}
If I open the form in edit mode I see all fields are filled with data but the testId field is filled with incorrect value(by default it fills with first element from array). The print_object($this->_data) 

stdClass Object

(

    [id] => 6

    [name] => История - Контр тест №1

    [competencyid] => 6

    [testid] => 32

    [courseid] => 2

)


I understand if I change [testid] on [id_testid_0] then correct value(i.e. 32) will inserted into the field. How to correct to process data in this case?

Average of ratings: -
In reply to Viktor Merinov

Re: The correct way to work with data in elements array

by Viktor Merinov -

$repeateloptions['testid']['default'] = $this->course_id; 
it solved the problem

In reply to Viktor Merinov

Re: The correct way to work with data in elements array

by Viktor Merinov -

It does not work for the next elements after clicking 'Add fields' button. What is wrong?


Attachment Screenshot_1.png