optional_param / required_param and arrays

optional_param / required_param and arrays

by Rob Wohleb -
Number of replies: 8
PHP supports the ability to create forms with grouped elements. They automatically become arrays inside of PHP. Here is how I must currently do it if I am trying to get the output from an input with the name of 'a[b]'.

$a = optional_param('a', NULL, NULL);
$b = clean_param($a['b']);

The clean_param function used in the optional_param and required_param functions does not support arrays, thus I must pass NULL as an option to get raw output. Then I have to manually clean 'b'. It would be nice if optional_param and required_param would allow me to get at 'a[b]' directly. It would also be nice if clean_param could recursively clean an array. This way I could just get a clean version of 'a'.

Did I miss something in this analysis? Is there currently a way to achieve what I want?

~Rob

Average of ratings: -
In reply to Rob Wohleb

Re: optional_param / required_param and arrays

by Martín Langhoff -

PHP supports the ability to create forms with grouped elements.

We are not using that technique in Moodle, because it has several issues, the main one being (IMHO) that it is not HTML/XHTML compliant. Square brackets are not valid form element names. See http://www.w3.org/TR/html4/types.html#type-id

In reply to Martín Langhoff

Re: optional_param / required_param and arrays

by Samuli Karevaara -
Brackets in input name attribute values are valid. This is a common misunderstanding. The link you gave mentions rules for tokens of SGML type NAME. A big but: the value of the name attribute for the input element is of type CDATA. The fact that it's name "name" matches another SGML data type is misleading.

See the XHTML DTD at http://www.w3.org/TR/xhtml1/dtds.html, relevant snippet below:
<!--ELEMENT input EMPTY-->     <!-- form control -->
<!--ATTLIST input
%attrs;
%focus;
type %InputType; "text"
name CDATA #IMPLIED
value CDATA #IMPLIED
checked (checked) #IMPLIED
disabled (disabled) #IMPLIED
readonly (readonly) #IMPLIED
size CDATA #IMPLIED
maxlength %Number; #IMPLIED
src %URI; #IMPLIED
alt CDATA #IMPLIED
usemap %URI; #IMPLIED
onselect %Script; #IMPLIED
onchange %Script; #IMPLIED
accept %ContentTypes; #IMPLIED
-->
You can also try to validate the attached example.
Average of ratings: Useful (1)
In reply to Samuli Karevaara

Re: optional_param / required_param and arrays

by Samuli Karevaara -
One thing that I have used this array trick with: Having user/group permissions as integers with each bit (yes/no) defining a different action (read/write/and so on). These integers can be automatically then split into checkboxes like permissions[bitposition] and the joined back together at the form handler.
In reply to Rob Wohleb

Re: optional_param / required_param and arrays

by John Papaioannou -
I didn't know about the compliance thing, it would sure be a major pain to work around it in forms with lots of checkboxes in them (it spoils the input name="id[]" trick). mixed

As for moodlelib, simply put Moodle doesn't do that thing as a matter of course so it's not essential to support it. If you want to do it in your own code (e.g. a custom module), you can write wrappers around optional_param & co and use those. wink
In reply to John Papaioannou

Re: optional_param / required_param and arrays

by Rob Wohleb -
Yes, the compliance thing is a pain. Too bad it is built into PHP and such a nice feature smile

I was hoping to avoid custom functions, but I guess that is the best bet to stay as close to the coding guidelines as possible.

In reply to Rob Wohleb

Re: optional_param / required_param and arrays

by Matthew Switlik -

A time traveling developer from the year 2012 here!

In Moodle v2.2 the correct way is to use required_param_array()

Average of ratings: Useful (2)