mform: it should be nice to kwon you

mform: it should be nice to kwon you

by Daniele Cordella -
Number of replies: 16
Picture of Core developers Picture of Plugin developers
Following the madness of a Moodle developer
I started my first module creation.

I found all I needed to start with module creation but not the same about moodle forms.
I tried to look inside moodle glossary php code and I found something but... which one is the API I have to browse in order to be more confident with moodle forms?

I would like to learn what is the minimum to start, how to create a "fixed" block of field to add to a different generic form (as Moodle already does), how to add a check box close to a field to see the password and not asterisks (as Moodle already does), how to, how to, how to...

Where have I to go?

Thank you in advance

Average of ratings: -
In reply to Daniele Cordella

Re: mform: it should be nice to kwon you

by Adam Zapletal -
I've always used the following wiki page and the others in the formslib category:

http://docs.moodle.org/en/Development:lib/formslib.php_Form_Definition


Average of ratings: Useful (3)
In reply to Adam Zapletal

Re: mform: it should be nice to kwon you

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers

I am trying to create a standard moodle form displaying just a field.

By pressing the submit button what I expect is to ALWAYS return to the form page showing a feedback like... "you entered <<field_content>>"

AND

with the field of the moodle form EMPTY.

To get this I changed the standard explanation provided in doc.moodle.org by changing

$mform = new yourmod_formfunction_form();//name of the form you defined in file above.
if ($mform->is_cancelled()){
} else if ($fromform=$mform->get_data()){
} else {
$mform->set_data($toform);
$mform->display();
print_footer($course);
}
to
$mform = new yourmod_formfunction_form();//name of the form you defined in file above.
if ($mform->is_cancelled()){
} else if ($fromform=$mform->get_data()){
} else {

}
$mform->set_data($toform);
$mform->display();
print_footer($course);

My problem is that whenever I get the form for the second time I always get the field STILL WITH the text I entered.

I even added the $mform->setDefault('field','defaultvalue'); but it defines the defualt of the new empty form... not of the re-loaded form.

Who can I dispose of the text I entered at the reload time?

Thank you in advance.

In reply to Daniele Cordella

Re: mform: it should be nice to kwon you

by Jamie Pratt -
Hi,

You can use :

$mform->setConstants(array('field'=>'defaultvalue'));


Jamie
In reply to Jamie Pratt

Re: mform: it should be nice to kwon you

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Fantastic. Thank you.
Do you believe me if I tell that... I spent two evenings (more or less, 6 hours) trying to get the right solution.arrossito

Thank you, Jamie.

Am I wrong if I say that... setConstants is not deeply described in docs.moodle.org? Eh?! pensieroso
In reply to Daniele Cordella

Re: mform: it should be nice to kwon you

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Reading the documentation provided in http://docs.moodle.org/en/Category:Formslib
I am able to add a read only field to a form only with:

$mform->addElement('text', 'currentstatus', get_string('currentstatus_label', 'newmodule'), array('disabled'=>'disabled'));

In this way I leave to the remote user the idea that... the field is available and he/she has only to find a way to enable it.
So I would like to show the content of my 'currentstatus' field using a static text like the one I can get using:

$mform->addElement('static', 'description', get_string('currentstatus_label', 'newmodule'), currentstatus);

but... it doesn't work.

I also tried:

$mform->addElement('hidden', 'currentstatus', 'currentstatus');
$currentstatus = $mform->getElementValue('currentstatus');
$mform->addElement('static', 'description',
get_string('currentstatus_label', 'newmodule'), '$currentstatus');

but it still doesn't work.
What am I supposed to use?

Sorry If I always ask. triste
Thank you in advance.
In reply to Daniele Cordella

Re: mform: it should be nice to kwon you

by Jamie Pratt -
"So I would like to show the content of my 'currentstatus' field using a static text"

Isn't the content already visible in the text box?? It should be although the text box is disabled.
In reply to Jamie Pratt

Re: mform: it should be nice to kwon you

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Yes it is, but, IMO, it is not so nice to see in a text box... because it seems you can edit it even if this is not true.
In reply to Daniele Cordella

Re: mform: it should be nice to kwon you

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
I am trying to build a mform to import a zipped folder.
Once the user push the submit button I want to start the following check.
// 1. It selected file has to be a zip file
// 2. The zip file has to contain only a folder (or only folders if more than ht per time is imported)
// 3. The folder has to have a name not present in the repository of files of this user and course

To build the form I added:
$mform->addElement('file', 'zippedfoldertoupload', get_string('attachment', 'forum'));
$mform->addRule('zippedfoldertoupload', null, 'required');
in the function definition() of the class
class mod_newmodule_uploadzip_form extends moodleform {

To perform the validation I was thinking to use the function validation($data) {

At this point I have three problems:
1: why do inside the function "validation" it is not defined the array item: $data('zippedfoldertoupload')?

If I change
$mform->addElement('file', 'zippedfoldertoupload', get_string('attachment', 'forum'));
$mform->addRule('zippedfoldertoupload', null, 'required');
to
$mform->addElement('text', 'zippedfoldertoupload', get_string('attachment', 'forum'), null);

in the function validation($data) { the array item: $data('zippedfoldertoupload') is well defined.
Is this a bug?

2: How am I supposed to use the
$this->set_upload_manager(new upload_manager('httoimport', true, false, $COURSE, false, 0, true, true, false));
What does each parameter, passed to upload_manager(), mean?
What is this class suposed to do?

3. which function (array element ??) does return me the path of the selected file inside the function validation($data)?

I am sorry for this questions but I was not able to find their answers in moodledocs.
If you know them in moodledocs, simply redirect me to them on the web.

TIA
In reply to Daniele Cordella

Re: mform: it should be nice to kwon you

by Jamie Pratt -
See the bottom of this page To learn to use the upload manager you could search moodle for uses of set_upload_manager and read the code.
In reply to Jamie Pratt

Re: mform: it should be nice to kwon you

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Sorry Jamie if I still ask.
At the bottom of
http://docs.moodle.org/en/Development:lib/formslib.php_Validation
I found:
$errors = parrent::validate($data, $files);
Following the outcome of my investigation I believe that...
  1. parrent
    needs to be changed to
    parent
    (And I already did it as you should know)
  2. validate
    should be replaced by
    validation
    but I am not brave enough to change it by myself. Am I right?
TIA