General developer forum

Own mod_form.php

This discussion has been locked because a year has elapsed since the last post. Please start a new discussion topic.
Picture of Silvan Voser
Own mod_form.php

Hi everybody,

I need to create an own edit page for an activity. So if I click in the course on "Update", I get to this page.

I am new to moodle and it would be great if you can give me some hints, documentation or advices how I can implements my functions in the best way. Of course I don't expect a full solution wink.

These are my questions:

  • How can I define own / new functions, not listed here? For example I can make $mform->add_myown_customized_editor();
  • If I save the formular, to where will the information from the fields go? (Later I need to process the values there and put them into MySQL database)
  • I need to check the values in the input fields, how can I check them with a moodle function?
  • How can I get values from the MySQL database for already existing exercises? For example I have a textarea with text. If I would like to change the text now, I need somehow to get the existing text from the MySQL database

Thanks for your help and regards,


Average of ratings: Useful (1)
Re: Own mod_form.php
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

If you are wanting to create your own settings form, then take a look at the file mod_form.php in any of the existing Moodle activities (mod/page/mod_form.php is a fairly straightforward example).

Validation of input can be done via a 'validation()' function in the form (search for examples) or can be handled via 'addRule' calls in the form definition (again, search for examples)

The data is returned to your activity via the ACTIVITYNAME_add_instance and ACTIVITYNAME_update_instance functions in your activity's lib.php. e.g. mod/page/lib.php: page_add_instance() and page_update_instance().

If you really need a non-standard form element, then you can copy what is done in lib/form/*.php, but you will also need to somewhere call MoodleQuickForm::registerElementType (see lib/formslib.php - scroll down to the bottom). However, I'd recommend not doing this unless you really have to (the built-in types cover 95% of use cases).

Moodle handles initialising the form elements for you, assuming you give the fields in the database the same name as the form elements.

Average of ratings: -
Picture of Silvan Voser
Re: Own mod_form.php

Thanks for your very fast reply, it helped me to go further.

I defined now a button to repeat the form. Did it according to:

My question is now, if there are already values for the forms in the MySQL database, how can I display them in the forms? Is there already a function in moodle for this?

Because I cannot display them now as I expected to do it, because repeat elements start automatically the index at 0. If I have already a form from the MySQL database with the ID 0, then I have duplicated IDs, if I would repeat the new form.

Thanks and regards,


Average of ratings: Useful (1)
Picture of Marcus Green
Populating repeat_elements fields with db data
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers

Did you ever come up with an answer to this? I am trying to work out how to populate fields that are generated from a call to repeat_elements with the associated data. I have generated the fields from count of the records but I cannot work out how to put the data in. I can see this is very similar to how adding hints or other arbitrary repeated fields works but I cannot see where the data is poked in.



Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: Populating repeat_elements fields with db data
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

The question editing forms give examples of this.

Average of ratings: -