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
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
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:
See the XHTML DTD at http://www.w3.org/TR/xhtml1/dtds.html, relevant snippet below:
<!--ELEMENT input EMPTY--> <!-- form control -->You can also try to validate the attached example.
<!--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
-->
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.
Hmmm. Something to learn here, thanks!
I went back to the html4 spec pages, trying to find the relevant page where the input element was described, and here it is: http://www.w3.org/TR/html4/types.html#type-id with the name attribute clearly marked as CDATA.
I went back to the html4 spec pages, trying to find the relevant page where the input element was described, and here it is: http://www.w3.org/TR/html4/types.html#type-id with the name attribute clearly marked as CDATA.
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).
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.
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.
Yes, the compliance thing is a pain. Too bad it is built into PHP and such a nice feature
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.
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.
A time traveling developer from the year 2012 here!
In Moodle v2.2 the correct way is to use required_param_array()
Thanks!