Arrays & optional_param()

Arrays & optional_param()

by Jordi Piguillem -
Number of replies: 8
Hi all,

This is a piece of a NWiki form:

<input type="submit" name="dfform[save]" value="<?php print_string('save','wiki');?>" />
<input type="submit" name="dfform[preview]" value="<?php print_string('preview');?>" />
<input type="submit" name="dfform[cancel]" value="<?php print_string('cancel');?>" />

Some people of DFWikiTeam need to do something like:

$variable = $_POST['dfform']['save'];

How can they do it using optiona_param()? I have found only two solutions:
  • optional_param('dfform',null,PARAM_RAW). Not good idea...
  • Try to hack first param: something like optional_param("'dfform']['save'",-1,PARAM_INT). Not nice...

Thanx for your help

DFWikiTeam, Pigui
Average of ratings: -
In reply to Jordi Piguillem

Re: Arrays & optional_param()

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Why use arrays in parameters?  It doesn't seem to have any advantages and just makes things complicated.

Just use:

  <input type="submit" name="save" value="<?php print_string('save','wiki');?>" />
  <input type="submit" name="preview" value="<?php print_string('preview');?>" />
  <input type="submit" name="cancel" value="<?php print_string('cancel');?>" />
In reply to Martin Dougiamas

Re: Arrays & optional_param()

by Jordi Piguillem -
I think that people who are working on it will do:

optional_param('dfform',null,PARAM_RAW);
Call clean_param() function for every element in the array afterwards.

Apply your solution involves not only changing all forms in NWiki, lot of code would had to be rewritten.
Thanx for your time.

DFWikiTeam, PIgui.
In reply to Jordi Piguillem

Re: Arrays & optional_param()

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
sad This doesn't make me any more confident about the maintainability of the nwiki code ...
In reply to Martin Dougiamas

Re: Arrays & optional_param()

by Jordi Piguillem -
Don't worry, false alarm...
I mean lots of 'find/replace' not rigorous code rewriting.

DFWikiTeam, Pigui
In reply to Martin Dougiamas

Re: Arrays & optional_param()

by José Rama -
Occam's razor:pensativo

entia non sunt multiplicanda praeter necessitatem (entities should not be multiplied beyond necessity.)
In reply to Jordi Piguillem

Re: Arrays & optional_param()

by Mohd Abdullah Al Nasser -

I know this is an old post but if anyone is still looking, the new way of doing it is :

$variable =optional_param_array('dfform', null, PARAM_INT);
Average of ratings: Useful (4)