Get strange warning using multi-choice parameters with optional_param, worked in 1.9

Get strange warning using multi-choice parameters with optional_param, worked in 1.9

by Per Ekström -
Number of replies: 6

Hi!

As you may or may not have seen from my previous threads, I'm slowly migrating a large site from 1.9 to 2.3. Things are going fairly ok, until now when I'm stuck at a very inconvenient bug.

I have developed my own custom filters for our own things, that lets you choose, say, ten courses and ten different teachers using multi-select boxes, as can be seen here, but now I get this error message:

Invalid array parameter detected in required_param(): courses
line 520 of /lib/moodlelib.php: call to debugging()
line 368 of /test/views/support.php: call to required_param()
line 130 of /test/index.php: call to include()

This is very annoying, since it worked in 1.9 and now for some reason seems broken. The relevant parts of my code:

//call with index.php?courses[]=1&courses[]=2&courses[]=3 etc

$courses=optional_param('courses',array(),PARAM_INT);

foreach ($courses as $c) {
    // Do stuff here
}

Calling optional_param with 'courses[]' instead of 'courses' gets rid of the error messages, but returns an empty array. Everything works fine except for the error message. Any idea why I get that, and how I can get rid of it?

Average of ratings: -
In reply to Per Ekström

Re: Get strange warning using multi-choice parameters with optional_param, worked in 1.9

by Andrew Normore -
call with index.php?courses[]=1&courses[]=2&courses[]=3 etc

I wonder if you should be URL encoding? Do a var_dump($courses) and post the results please!
In reply to Andrew Normore

Re: Get strange warning using multi-choice parameters with optional_param, worked in 1.9

by Per Ekström -

I would like to use URL parameters since it's a filter and the biggest advantage I have is that I can simply send an URL to a person "Hey, this looks wierd, check this out!" instead of having to explain that it's these five courses he needs to look on.

Anyhow, doing 'courses':

var_dump(optional_param('courses',array(),PARAM_INT));

Yields this: array(1) { [0]=> int(1) }

And if I do 'courses[]':

var_dump(optional_param('courses[]',array(),PARAM_INT));

Yields this: array(0) { }

In reply to Per Ekström

Re: Get strange warning using multi-choice parameters with optional_param, worked in 1.9

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

In 2.x ou should use optional_param_array if you expect to get back multiple results like this. This was documented in the release notes.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Get strange warning using multi-choice parameters with optional_param, worked in 1.9

by Andrew Normore -

^^ This is probably the problem then, can't believe I missed it. Thanks Tim.

In reply to Tim Hunt

Re: Get strange warning using multi-choice parameters with optional_param, worked in 1.9

by Per Ekström -

I knew it was something simple like that! Thanks Tim, had forgotten about it. smile