Quickform using foreach loop possible?

Quickform using foreach loop possible?

by Josh Wilkinson -
Number of replies: 4

Is it possible to use foreach loops in a quickform extension to add elements to a form using a this $mform->addElement('checkbox', 'check'.$count, $dis);? This is contained in a foreach loop. All the elements appear and function onscreen but the fields do not get posted when the form is submitted? Anyone ran into this problem before?

 

Average of ratings: -
In reply to Josh Wilkinson

Re: Quickform using foreach loop possible?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

This works. See, for example the quiz settings form. mod/quiz/mod_form.php, the add_review_options_group method and the place where it is called.

See also the formslib repeat_elements method, and places where that is used (e.g. question editing forms).

In reply to Josh Wilkinson

Re: Quickform using foreach loop possible?

by Rob P -

I'm experiencing the same thing! 

I've got advanced check boxes that I want to make in the foreach loop that appear in the form but then don't get posted when submitted.

They work fine when I make them outside the foreach loop but as soon as I try and create it within the loop it just doesn't work. I can't work out why.

In reply to Josh Wilkinson

Re: Quickform using foreach loop possible?

by Rob P -

I still haven't found a solution to this problem.


Here is the code where I make the form:

function definition(){

  $mform=&$this->_form;

  $arrgroup = array();

  $arrgroup[] = $mform->createElement('hidden', 'user',$this->_customdata['viewuser']);

  $arrayRecieved = $this->_customdata['dataArray'];

  $countthis = 0;

  foreach((array) $arrayRecieved as $datapoint){

    $arrgroup[] = $mform->createElement('advcheckbox', 'delop'.$countthis, $arrayRecieved[$countthis]->id, "Outcome: ".$arrayRecieved[$countthis]->rownum.", Year: ".$arrayRecieved[$countthis]->year.", Grade: ".$arrayRecieved[$countthis]->grade);

    $countthis++;

  }

  $arrgroup[] = $mform->createElement('submit','submit',"Delete Data Points");

  $mform->addElement('group','coursegroup','Delete Data Points',$arrgroup,array(''),false);

  $mform->setType('Delete Data',PARAM_INT);

}


It seems to work fine as it appears on the form fine:



Here is the code where I try to read it when it is submitted:

if($delete_form->get_data()){

  print_object($delete_form->get_data());

}


The ticked or unticked advanced checkbox values aren't listed when I try printing out the returned data, only everything else in the form is returned.



I'm sure I'm probably doing something really simple wrong and I've tried different variations but still to no avail. Anyone able to see what I'm doing wrong?

In reply to Rob P

Re: Quickform using foreach loop possible?

by Ronan Choiselat -

Issue is a quite old, but I encounter the same problem and I've just found a solution.


In my case, when I load the form page the first time, I pass an extra parameter. I use this parameter to retrieve data from DataBase, and fill my customdata object.

Like you, I use data contains in my customdata object to loop and generate checkbox. And it works fine.

When I validate the form, form page is load again, but with no extra parameter. So The customdata object is not correctly fill, and the form does not get the right data from the customdata object. That's why get_data() does not retrieve values for fields define in the loop (they are not instanciate due to empty customdata object)

To solve this, I add a field to my customdata object to add the value of the extra parameter, that I use to setup a hiddenfield. When the form is validate, when the page is load again, I check my extra parameter, if it has its default value, I check content of $_POST to retrieve value contains in the hidden field. I think the solution is dirty, but I don't know how to pass extra parameter through a form without using a hidden field.

If you don't want to manage your extra parameter, you also could get your data directly form $_POST rather from get_data(), but you loose all benefits of moodleform using (data check, character escape, etc...).


If my explaination remain unclear, you could check this thread on stackoverflow, which treat a very similar issue.