Need to retrieve arrays of arrays in optional_param_array()

Need to retrieve arrays of arrays in optional_param_array()

by Joseph Rézeau -
Number of replies: 9
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Whilst upgrading my REGEXP question type to Moodle 2.3 (dev), just came across this problem.

I have read MDL-26796 "stop accepting arrays in required_param() and optional_param()".

In my edit_regexp form I need to retrieve the question's feedback arrays. If I use optional_param(), moodle complains that "clean_param() can not process arrays, please use clean_param_array() instead."

If I use optional_param_array(), I get the same error message. It took me some time and looking into moodlelib to realize that the problem is this:

in function optional_param_array() : Note: arrays of arrays are not supported, only alphanumeric keys with _ and - are supported.

This worked fine before Moodle 2.3. Please advise what I can do now.

In the question edit form, feedbacks are in arrays of arrays like see attached.

Joseph

EDIT.- Of course I could always use $_POST[], but I'd rather use the Moodle API if at all possible.

Attachment 27-02-2012 18-50-15.jpg
Average of ratings: -
In reply to Joseph Rézeau

Re: Need to retrieve arrays of arrays in optional_param_array()

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

Inside a moodle form, you really should not need to use optional_param at all.

In reply to Tim Hunt

Re: Need to retrieve arrays of arrays in optional_param_array()

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

@Tim,

You are probably right, but it so happens that I need it in relation with this development: http://moodle.org/mod/forum/discuss.php?d=192846

Joseph

In reply to Joseph Rézeau

Re: Need to retrieve arrays of arrays in optional_param_array()

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

I sort-of guessed that.

Here is an evil idea. What about if you don't declare that button a 'no-submit' button, but instead, make it a submit  button, but if the button is pressed, make sure the validation returns some errors so the form is not submitted.

I'm not sure that will acutally work, but it is worth a try.

(I was going to say "Look at calculatedsimple" form, but that seems to get it wrong at the moment. It uses optional_param in the form code. Oh dear!)

In reply to Tim Hunt

Re: Need to retrieve arrays of arrays in optional_param_array()

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

1.- Tim: "(I was going to say "Look at calculatedsimple" form, but that seems to get it wrong at the moment. It uses optional_param in the form code. Oh dear!)"

Yes it does.tongueout And it's also used in edit_multianswer_form.php.

2.- I have finally sorted everything re the development mentioned in http://moodle.org/mod/forum/discuss.php?d=192846 and it all went fine until the optional_param_array() change in moodle 2.3. Why can't the Moodle API deal with arrays of arrays, surely this must be needed at times?

Joseph

In reply to Joseph Rézeau

Re: Need to retrieve arrays of arrays in optional_param_array()

by Itamar Tzadok -

Not so elegant as this may be, how about serialize/unserialize or flatten/unflatten the array of arrays? smile

In reply to Itamar Tzadok

Re: Need to retrieve arrays of arrays in optional_param_array()

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

@Itamar: where do you suggest that the serialize/unserialize function be used? In moodlelib?

@Tim and other developers: if the only way to get out of this conundrum is for me to use $_POST, is this OK or are there adverse effects?

Joseph

In reply to Joseph Rézeau

Re: Need to retrieve arrays of arrays in optional_param_array()

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

Suggestion from Petr Skoda: "why not use old data_submitted() and process data with clean_param() instead?"

In reply to Tim Hunt

Re: Need to retrieve arrays of arrays in optional_param_array()

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

@Tim,

Yes, data_submitted() works fine for me. But why should I process the result with clean_param()?

Joseph

In reply to Joseph Rézeau

Re: Need to retrieve arrays of arrays in optional_param_array()

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

Well, for the usual reasons. To avoid XSS attacks. If you do $data=data_submitted() and are expecting $data[1][2] to be an integer, then it would be safer to do $data[1][2] = clean_param($data[1][2]) before you do antyhing else with that value.

Average of ratings: Useful (1)